Retrieve the current view name in ASP.NET MVC?

后端 未结 10 863
谎友^
谎友^ 2020-11-28 09:37

I have a partial view (control) that\'s used across several view pages, and I need to pass the name of the current view back to the controller - so if there\'s e.g. validati

10条回答
  •  無奈伤痛
    2020-11-28 10:13

    Easiest solution is using ViewBag.

    public ActionResult Index()
        {
            ViewBag.CurrentView = "Index";
            return View();
        }
    

    On the cshtml page

    @{
    var viewName = ViewBag.CurrentView;
    }
    

    Or,

    ((RazorView)ViewContext.View).ViewPath
    

提交回复
热议问题