使用 ASP.NET MVC 显示 FormCollection
我有一个在视图上使用 Html.BeginForm() 的表单。我在控制器中有一个 ActionResult 来处理该帖子。我需要的只是将结果返回到视图中。我可以启动新视图,但我不知道如何将数据传递给它,并且一旦到达那里我不知道如何显示它。这是我在 ActionResult 中的内容。
[HttpPost]
public ActionResult Index(FormCollection collection)
{
ViewBag.Title = "Confirm your order";
return View("OrderConfirmation", collection);
}
如果我只是执行 return View("OrderConfirmation");它将进入视图,所以我知道我已经成功了。我只是不知道如何传递数据。现在我将它强类型化为与表单相同的模型,这会导致错误,因为这个 FormCollection 显然不一样。如果我删除强类型行,上面的代码就可以工作,但我不知道如何在此时循环遍历集合。
感谢您的帮助。
I have a form using Html.BeginForm() on a view. I have in the controller an ActionResult to handle the post. What I need is to just kick back the results to a view. I can kick off the new view, but I don't know how to pass the data to it and once there I don't know how to display it. Here's what I have in the ActionResult.
[HttpPost]
public ActionResult Index(FormCollection collection)
{
ViewBag.Title = "Confirm your order";
return View("OrderConfirmation", collection);
}
If I just do a return View("OrderConfirmation"); it will go to the view so I know I got that working. I just don't know how to pass the data. Right now I have it strongly typed to the same model the form was which causes errors because this FormCollection is not the same obviously. If I remove the strongly typed line the above works, but I have no idea how to loop through the collection at that point.
Thanks for the help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 ViewModel 和强类型视图。然后您可以将模型传递到第二个视图。
ASP.NET MVC 将自动创建一个订单实例并填充发布的 FormCollection 中的属性。
Use a ViewModel and a stongly typed view. Then you can pass the model to the second view.
ASP.NET MVC will automatically create an order instance and fill the properties from the posted FormCollection.
首先不要使用FormsCollection,它太通用了。仅当您需要进行单元测试并访问 UpdateModel() 时才需要它。
绑定到模型类型或绑定到参数:
或
在顶部的视图中指定
First don't use FormsCollection, its too generic. You only need it if you need to unit test and access UpdateModel().
Bind to a model type or bind to params:
or
in your view at the top specify