简体   繁体   中英

Can a ViewBag.Something transport data from view to the controller as well?

I have the following case:

@model MvcApp.Models.MyAppModel
@{ ViewBag.ActionName = this.Context.Request.RequestContext.RouteData.Values["action"]; }

using the debugger i checked and i am certain that Viewbag.ActionName recieves its value. However in the controller ViewBag is always empty. WHY? i am using this between these requests only ( meaning that i always respect this order, from this view i click an actionlink that always takes me to delete. Please enlighten me. Cheers.

[HttpGet]
public ActionResult Delete(int id)
{
    string actionName = ViewBag.ActionName;

any details you need just ask.

The data in the view bag is only valid for the duration of a single request. But your talking about two requests: the first one renders the page and the second one processes the Delete action. That's why the view bag is initially empty in the controller.

The flow of a request is: First the controller, then the View.

So the answer is no, what you do in the view, can not carry over to the action/controller.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM