ASP。NET MVC返回一个不同的视图

时间:2022-11-24 11:22:17

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");

我有一个视图,它包含一个表单,表单post和数据被处理等等,然后我想返回视图索引,所以返回视图(“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不存在,我感觉在索引()下的控制器代码没有被处理,它添加了对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内容两个控制器

2 个解决方案

#1


57  

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”),就完成了。我希望我理解你的意思。

#2


6  

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”);然后用它需要的任何参数调用索引方法。然后该方法将通过索引视图填充ViewData。

#1


57  

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”),就完成了。我希望我理解你的意思。

#2


6  

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”);然后用它需要的任何参数调用索引方法。然后该方法将通过索引视图填充ViewData。