繁体   English   中英

.Net MVC控制器使用相同的cshtml作为视图使用不同的路由名称

[英].Net MVC Controller different route name using the same cshtml as view

在.NET MVC中,我看到它始终遵循以下命名约定:控制器中的RouteName,它是一个名为“ RouteName.cshtml”的视图。

我的问题是如何使用带有不同RouteName的SAME cshtml文件?

默认的View()方法默认与Action名称匹配,但是您可以使用View(string)方法来指定所需CSHTML的路径,如以下示例所示:

// in action method that is *not* RouteName
return View("RouteName");

您可以使用带有partials的CSHTML块; 根据您要解决的问题,这可能是一个更好的解决方案。

您可以编辑问题并添加更多上下文吗? 为什么需要通过多个ActionResult方法呈现相同的cshtml? 你想达到什么目的?

例如,您有一个名为About.cshtml的视图,您有2个操作方法,并且两个方法都使用About.cshtml

所以你可以尝试

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM