ASP.NET MVC 2 - POST 后 ViewData 为空

发布于 2024-09-03 12:31:15 字数 309 浏览 9 评论 0原文

我真的不知道在哪里寻找错误...情况:我有一个 ASPX 视图,其中包含一个表单和一些输入,当我单击“提交”按钮时,所有内容都会 POST 到我的一个 ASP。 NET MVC 操作。

当我在那里设置断点时,它被正确命中。当我使用 FireBug 查看发送到操作的内容时,我正确地看到 data1=abc&data2=something&data3=1234。

但是,我的操作方法中没有任何内容。 ViewData 为空,没有 ViewData["data1"] 或任何其他可以显示数据到达的内容。

怎么会这样呢?我可以从哪里开始寻找错误?

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.

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.

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.

How can this be? Where can I start looking for the error?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梦回梦里 2024-09-10 12:31:15

当从控制器到视图时,ViewData 是相关的。它不会回发。

你需要你的操作方法看起来像这样

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

然后(模型?参数?)绑定应该为你处理事情

ViewData is relevant when going from the controller to the view. 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

韶华倾负 2024-09-10 12:31:15

尝试修改您的操作以接受 FormCollection:

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

Try modifying your Action to accept FormCollection:

public ActionResult DoSomething(FormCollection fc)
{
     System.Diagnostics.Debug.Writeline(fc["data1"]);
}
甜是你 2024-09-10 12:31:15

如果您想查看发布到视图的内容,请接受 FormCollection 作为参数,或将表单元素直接绑定到模型。像这样:

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

另外,请参阅此问题

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. Like so:

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

Also, see this question.

神妖 2024-09-10 12:31:15

试试这个:

Request["data1"]

或者

Request.Form["data1"]

或者

[HttpPost]
public ActionResult YourPostAction(object data1)

或者

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文