I feel stupid asking this but I cant seem to get a partial view rendering in a page.
I have created a partial view that im trying to load into my index page. I have called my pv _BusinessDetails basically its a view that returns some customer data.
My pv looks like
@model MyMVC.Models.BusinessModel
<div class="grid">
<div class="grid-header">
<div class="gh-l"></div>
<div class="gh-m">Business Details</div>
<div class="gh-r"></div>
</div>
<div class="grid-row">
<label class="labelBold">Busines Name</label>
<label>@Model.BusinesName</label>
</div>
</div>
From my index page I am trying to call the pv using
@Html.Partial("_BusinessDetails")
which fails so if I add
@Html.Partial("_BusinessDetails",new MyMVC.Models.BusinessModel())
The partial view is loaded however with no data as the controller isn’t been hit. In my controller I have tried
public ActionResult _BusinessDetails()
{
return PartialView("_BusinessDetails");
}
public PartialViewResult _BusinessDetails()
{
return PartialView("_BusinessDetails");
}
However neither of them are hit. What have I done wrong?
When rendering a partial view and passing a view model, that view model should already be populated. No controllers/action methods are invoked when using
@Html.Partial().Since you are using this strongly-typed partial view on your home page, consider building its view model in your
HomeController‘sIndex()method. Is your index page strongly-typed as well? If so, you can add your partial view’s view model as a property of your index page’s view model, and pass that when calling@Html.Partial().On your index page, it would look something like:
If your index page is not strongly-typed, you can use the
ViewBagobject or you can strongly-type it toMyMVC.Models.BusinessModeland use@Html.RenderPartial("_BusinessDetails", Model)(which, while simple, could cause confusion).Rachel Appel has a nice blog post, as does Mike Brind, if you would like more information.