Asp.Net Mvc - How to have a “controller” in shared view

后端 未结 5 2065
暖寄归人
暖寄归人 2020-12-24 11:45

I have a shared view in my _Layout.cshtml for my header named \"_Header.cshtml\".

I would like to display text and image from the database, so I need my controller t

5条回答
  •  忘掉有多难
    2020-12-24 12:01

    In your contoller action you could specify the name of the view:

    public class MenuController : Controller
    {
        [ChildActionOnly]
        public ActionResult Header()
        {
            var model = ... // go to the database and fetch a model
            return View("~/Views/Shared/_Header.cshtml", model);
        }
    }
    

    Now in your _Layout.cshtml instead of @Html.Partial("_Header") do this:

    @Html.Action("Header", "Menu")
    

提交回复
热议问题