[英].Net MVC Controller different route name using the same cshtml as view
In .NET MVC, I see it always follows this naming convention: RouteName in the controller and it is a view called 'RouteName.cshtml'. 在.NET MVC中,我看到它始终遵循以下命名约定:控制器中的RouteName,它是一个名为“ RouteName.cshtml”的视图。
My question is how can I use SAME cshtml file with different RouteName? 我的问题是如何使用带有不同RouteName的SAME cshtml文件?
The default View() method defaults to matching the Action name, but you can use the View(string) method to specify that path to your desired CSHTML, like in the following example: 默认的View()方法默认与Action名称匹配,但是您可以使用View(string)方法来指定所需CSHTML的路径,如以下示例所示:
// in action method that is *not* RouteName
return View("RouteName");
You can re-use chunks of CSHTML with partials ; 您可以使用带有partials的CSHTML块; depending on the problem that you're trying to solve, this may be a better solution.
根据您要解决的问题,这可能是一个更好的解决方案。
Can you edit your question and add more context? 您可以编辑问题并添加更多上下文吗? Why do you need to render the same cshtml from more than one ActionResult method?
为什么需要通过多个ActionResult方法呈现相同的cshtml? What are you trying to accomplish?
你想达到什么目的?
For example, you have a view named About.cshtml You have 2 action methods and both of them are using About.cshtml 例如,您有一个名为About.cshtml的视图,您有2个操作方法,并且两个方法都使用About.cshtml
So you can try 所以你可以尝试
public ActionResult Index()
{
var model = "view model";
// Pass view model as the second parameter
return View("About", model);
}
public ActionResult About()
{
ViewBag.Message = "Your application description page.";
// Not need pass view name because view name and action method name are same (About
return View();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.