Many Partial in same View


To Display Multi Partial in same View
You can only return one value from a function so you can't return multiple partials from one action method.
If you are trying to return two models to one view, create a view model that contains both of the models that you want to send, and make your view's model the new ViewModel. E.g.

Your view model would look like:

public class ChartAndListViewModel

{

   public List<ChartItem> ChartItems {get; set;};

   public List<ListItem> ListItems {get; set;};

}

Then your controller action would be:

public ActionResult ChartList()

{

   var model = new ChartAndListViewModel();

   model.ChartItems = _db.getChartItems();

   model.ListItems = _db.getListItems();

 

   return View(model);

}

And finally your view would be:

@model Application.ViewModels.ChartAndListViewModel

 

<h2>Blah</h2>

 

@Html.RenderPartial("ChartPartialName", model.ChartItems);

 

@Html.RenderPartial("ListPartialName", model.ListItems);

 --------------------------------------------------------------------------------

Post a form with multiple partial views

@using (Html.BeginForm("TransactionReport", "Reports", FormMethod.Post, new {id="report_request"}))    {        ViewContext.FormContext.ValidationSummaryId = "valSumId";        @Html.ValidationSummary(false, "Please fix these error(s) and try again.", new Dictionary<string, object> { { "id", "valSumId" } });        @Html.Partial("_ReportOptions", Model.ReportOptions);        @Html.Partial("_TransactionSearchFields", new ViewDataDictionary(viewData) { Model = Model.SearchCriteria });    }

????

 

Partials are not the way to go here. You are looking for EditorTemplates, these are made for what you want. This case, your properties will be nicely bound to your model (that you will submit).

Your main View will have this form (note that you only have to use EditorFor instead of Partial; in this case, you probably will need to put that viewData parameter in the ViewBag or so):

@using (Html.BeginForm("TransactionReport", "Reports", FormMethod.Post, new {id="report_request"})){    ViewContext.FormContext.ValidationSummaryId = "valSumId";    @Html.ValidationSummary(false, "Please fix these error(s) and try again.", new Dictionary<string, object> { { "id", "valSumId" } });    @Html.EditorFor(model => model.ReportOptions);    @Html.EditorFor(model = Model.SearchCriteria });}

 

Now you only have to drag your partials to the folder ~/Shared/EditorTemplates/ and rename them to match the model name they are the editor templates for.

In the ~/Shared/EditorTemplates/ folder, make a new "view", example "SearchCriteria.cshtml". Inside, put as "model" the type of class you which to create an editor template for. Example (example class has properties Name and OtherCriteria):

@model MyNamespace.SearchCriteria<ul>    <!-- Note that I also use EditorFor for the properties; this way you can "nest" editor templates or create custom editor templates for system types (like DateTime or String or ...). -->    <li>@Html.LabelFor(m => m.Name): @Html.EditorFor(m => m.Name)</li>    <li>@Html.LabelFor(m => OtherCriteria): @Html.EditorFor(m => m.OtherCriteria</li></ul>

 

Some good reading about them:

https://www.growingwiththeweb.com/2012/12/aspnet-mvc-display-and-editor-templates.html

 


Did you find this article useful?



  • Annotation for validation

    Adding Validation to our Model We'll use the following Data Annotation attributes: Required – Indicates that the property is a required...

  • Display and Editor Templates - ASP.NET MVC

    When dealing with objects in an MVC app, we often want a way to specify how that object should be displayed on any given page. If that object is only ...