简体   繁体   English

ViewBag.Something是否也可以将数据从视图传输到控制器?

[英]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. 使用我检查过的调试器,我确定Viewbag.ActionName会收到它的值。 However in the controller ViewBag is always empty. 但是,在控制器中,ViewBag始终为空。 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. 但是您谈论的是两个请求:第一个请求呈现页面,第二个请求处理Delete动作。 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. 因此,答案是否定的,您在视图中所做的操作无法继承到动作/控制器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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