简体   繁体   English

ASP.NET MVC 2 - POST后的ViewData为空

[英]ASP.NET MVC 2 - ViewData empty after POST

I don't really know where to look for an error... the situation: I have an ASPX view which contains a form and a few input's, and when I click the submit button everything is POST'ed to one of my ASP.NET MVC actions. 我真的不知道在哪里寻找错误......情况:我有一个ASPX视图,其中包含一个表单和一些输入,当我点击提交按钮时,一切都被POST到我的一个ASP。 NET MVC操作。

When I set a breakpoint there, it is hit correctly. 当我在那里设置断点时,它被正确击中。 When I use FireBug to see what is sent to the action, I correctly see data1=abc&data2=something&data3=1234. 当我使用FireBug查看发送到操作的内容时,我正确地看到data1 = abc&data2 = something&data3 = 1234。

However, nothing is arriving in my action method. 但是,我的行动方法没有任何结果。 ViewData is empty, there is no ViewData["data1"] or anything else that would show that data arrived. ViewData为空,没有ViewData [“data1”]或其他任何可以显示数据到达的内容。

How can this be? 怎么会这样? Where can I start looking for the error? 我在哪里可以开始寻找错误?

ViewData is relevant when going from the controller to the view. 从控制器到视图时,ViewData是相关的。 It won't post back. 它不会回发。

you'll need your action method to look something like 你需要你的动作方法来看起来像

public ActionResult DoSomething(string data1, string data2, int data3) { ...

Then the (model? parameter?) binding should take care of things for you 然后(模型?参数?)绑定应该为你处理的事情

Try modifying your Action to accept FormCollection: 尝试修改您的Action以接受FormCollection:

public ActionResult DoSomething(FormCollection fc)
{
     System.Diagnostics.Debug.Writeline(fc["data1"]);
}

If you want to see what is posted to your View, accept FormCollection as a parameter, or bind your form elements directly to a model. 如果要查看发布到View的内容,请接受FormCollection作为参数,或将表单元素直接绑定到模型。 Like so: 像这样:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult PostToThisView(FormCollection formItems)
{
    var data1 = formItems["data1"];
    return View();
}

Also, see this question . 另外, 请看这个问题

try this: 尝试这个:

Request["data1"]

or 要么

Request.Form["data1"]

or 要么

[HttpPost]
public ActionResult YourPostAction(object data1)

or 要么

[HttpPost]
public ActionResult YourPostAction(ACLassThatHasData1Prop viewmodel)
//your view doesn't has to be strongly typed to the type of the parameter in this case

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

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