使用 ASP.NET MVC 4 从控制器调用另一个不同的视图 [英] Calling another different view from the controller using ASP.NET MVC 4

查看:34
本文介绍了使用 ASP.NET MVC 4 从控制器调用另一个不同的视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有提交按钮的视图 (Index.cshtml).单击提交按钮时,它会调用控制器 (TestController.cs) 内的操作 (Action01),因此在操作结束时,我想使用自定义视图模型作为参数返回调用者 (Index.cshtml) 视图.我该怎么做?

I have a view (Index.cshtml) with a submit button. When the submit button is clicked, it calls an action (Action01) within the controller (TestController.cs) so at the end of the action I want to return to the caller (Index.cshtml) view with a custom view model as a parameter. How do I do this?

第一次尝试使用 View("ViewName",model) 后的结果:

Results after first attempt using View("ViewName",model):

引发错误,因为操作在控制器 Test 中,因此返回时,它正在搜索 ViewsTestsIndex,而我的索引页面在 ViewsHomeIndex 中.

An error is raised, as the action is within the controller Test, so returning, it is searching for ViewsTestsIndex, and my Index page is in ViewsHomeIndex.

未找到视图索引"或其主视图,或者没有视图引擎支持搜索的位置.搜索了以下位置:

The view 'Index' or its master was not found or no view engine supports the searched locations. The following locations were searched:

~/Views/Test/Index.aspx
~/Views/Test/Index.ascx
~/Views/Shared/Index.aspx
~/Views/Shared/Index.ascx
~/Views/Test/Index.cshtml
~/Views/Test/Index.vbhtml
~/Views/Shared/Index.cshtml
~/Views/Shared/Index.vbhtml

最终解决方案:

我使用了 return View("ViewName", model),并且我更改了目录结构,因为这是问题所在.

Final solution:

I have used return View("ViewName", model), and I have changed my directories structure as it was the problem.

推荐答案

你可以直接返回一个不同的视图,如:

You can directly return a different view like:

return View("NameOfView", Model);

或者您可以制作局部视图并返回:

Or you can make a partial view and can return like:

return PartialView("PartialViewName", Model);

这篇关于使用 ASP.NET MVC 4 从控制器调用另一个不同的视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆