Asp.Net MVC Layout and Partial Views

consider two types that use the same layout, consisting of:

  • Left column containing "body" (which is filled differently by both views)
  • Right column that displays general information (passed through the model)

Instead of defining the correct part twice, I wondered if I could create a PartialView to directly link to the layout page.

The problem is that partial views implicitly inherit their models from the displayed view. And since each view has its own model, I end the mismatch of the type of model in the partial view.

From here I see two solutions:

  • I could insert the general part of the view model into the ViewBag. Unfortunately, this means that every view using this layout must implement this โ€œconventionโ€, but nothing warns the developer about this at compile time ...
  • I could use polymorphism so that each view model inherits from the same base class (edit or interface) that uses Partial Views. This will work to a certain extent, but will potentially exponentially increase complexity as soon as I get a second partial view in the same layout.

So here are the questions:

  • Am I correct with the assumptions above?
  • Do you see another opportunity?
  • Any return on this experience?

Thanks a lot, TB.

+7
source share
3 answers

Use Interface and implement it on two models, this is exactly what they are used for.

Here is an example of two different views using two different models that implement the interface. This is subtyping instead of ad-hoc polymorphism .

 public class ViewModelOne : IReusableView { public string Name { get; set; } public string Something { get; set; } public int ANumber { get; set; } } public class ViewModelTwo : IReusableView { public string Name { get; set; } public string Thing { get; set; } public string SomethingElse { get; set; } public int ANumber2 { get; set; } } public interface IReusableView { string Name { get; } } 

So, we have a really simple partial view: "InnerPartialView":

 @model TestIntegration.Models.IReusableView <div> @Model.Name </div> 

What is used in the home page and on the pages of this example controller:

  public class HomeController : Controller { public ActionResult Index() { ViewBag.Message = "Welcome to ASP.NET MVC!"; return View(new ViewModelOne() { Name = "hello", Something="sdfsdfs", ANumber = 1 }); } public ActionResult About() { return View(new ViewModelTwo() { Name = "hello 2", SomethingElse = "aaaddd", ANumber2 = 10, Thing="rand" }); } } 

Home view:

 @model TestIntegration.Models.ViewModelOne @{ ViewBag.Title = "Home Page"; } <h2>@ViewBag.Message</h2> <p> To learn more about ASP.NET MVC visit <a href="http://asp.net/mvc" title="ASP.NET MVC Website">http://asp.net/mvc</a>. @Html.Partial("InnerPartialView") </p> 

View View:

 @model TestIntegration.Models.ViewModelTwo @{ ViewBag.Title = "About Us"; } <h2>About</h2> <p> Put content here. @Html.Partial("InnerPartialView") </p> 
+7
source

When rendering a partial view, you can send it a model:

 @Html.RenderPartial(MVC.Partials.Views.Sidebar, Model.SideBarModel); 

This way you can send data as part of the parent model, which is the model for the partial sidebar.

+3
source

In partial views, models are of type dynamic , so you do not need to know what type they are. However, you just need to make sure that the model has the property you need. In other words, you can use Model.MyProperty or Model.MyProperty as MyPropertyType when using Html.Partial .

0
source

All Articles