asp.net-mvc 使用 ASP.NET MVC 4 从控制器调用另一个不同的视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18910530/
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
Calling another different view from the controller using ASP.NET MVC 4
提问by Ralph
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?
我有一个带有提交按钮的视图 (Index.cshtml)。单击提交按钮时,它会调用控制器 (TestController.cs) 内的操作 (Action01),因此在操作结束时,我想使用自定义视图模型作为参数返回调用者 (Index.cshtml) 视图. 我该怎么做呢?
Results after first attemptusing View("ViewName",model):
结果后的第一次尝试使用视图(“视图名”,模型):
An error is raised, as the action is within the controller Test, so returning, it is searching for \Views\Tests\Index, and my Index page is in \Views\Home\Index.
引发错误,因为操作在控制器 Test 中,因此返回时,它正在搜索 \Views\Tests\Index,而我的索引页面在 \Views\Home\Index 中。
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
Final solution:
最终解决方案:
I have used return View("ViewName", model), and I have changed my directories structure as it was the problem.
我已经使用了return View("ViewName", model),我已经改变了我的目录结构,因为这是问题所在。
回答by Neel
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);
回答by Jatin patil
To return a different view, you can specify the nameof the view you want to return and modelas follows:
返回不同的视图,您可以指定name要返回和观点model如下:
return View("ViewName", yourModel);
if the view is in different folder under Viewsfolder then use below absolute path:
如果视图位于Views文件夹下的不同文件夹中,则使用以下绝对路径:
return View("~/Views/FolderName/ViewName.aspx");
回答by RaBz
You have to specify the name of the custom view and its related model in Controller Action method.
您必须在 Controller Action 方法中指定自定义视图的名称及其相关模型。
public ActionResult About()
{
return View("NameOfViewYouWantToReturn",Model);
}
回答by Diako Hasani
public ActionResult Index()
{
return View();
}
public ActionResult Test(string Name)
{
return RedirectToAction("Index");
}
Return ViewDirectly displays your viewbut
Return View直接显示你的view但是
Redirect ToActionAction is performed
Redirect ToAction动作被执行
回答by César León
Also, you can just set the ViewName:
此外,您可以只设置 ViewName:
return View("ViewName");
Full controller example:
完整控制器示例:
public ActionResult SomeAction() {
if (condition)
{
return View("CustomView");
}else{
return View();
}
}
This works on MVC 5.
这适用于 MVC 5。

