C# ASP.NET MVC 返回不同的视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/317611/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ASP.NET MVC return a different view
提问by Tablet
I have a view which contains a form, the form posts and the data gets processed etc, then I want to return the view Index, so return view("Index");
我有一个包含表单、表单发布和数据处理等的视图,然后我想返回视图索引,所以返回视图(“索引”);
however this will then complain about my ViewData not existing, I get the feeling that the controller code under Index() isn't being processed which adds the list it requires to the ViewData, anyone know what's wrong?
但是这会抱怨我的 ViewData 不存在,我觉得 Index() 下的控制器代码没有被处理,它将它需要的列表添加到 ViewData 中,有人知道出了什么问题吗?
Thanks
谢谢
edit: Apparently it's done to prevent recursion.. in which case, I'm lost as to what to do without repeating all my ViewData stuff both Controllers
编辑:显然这样做是为了防止递归......在这种情况下,我不知道该怎么做而不重复我所有的 ViewData 东西两个控制器
采纳答案by CodeClimber
I think you should have two actions: one that processes the form submission, and another one that collects data for the view.
Once the form has been processed, you call return RedirectToAction("Index")
and you are done.
I hope I understood what you meant by this.
我认为你应该有两个动作:一个处理表单提交,另一个收集视图数据。表格处理完毕后,您可以致电return RedirectToAction("Index")
并完成。我希望我明白你的意思。
回答by Matthew
If your Index method on the controller does a return View("Index");
then just call the Index method with any parameters it requires. Then the method will populate the ViewData reuired by the Index View.
如果控制器上的 Index 方法执行 areturn View("Index");
则只需使用它需要的任何参数调用 Index 方法。然后该方法将填充索引视图所需的 ViewData。