Asp.net mvc - transfer partial data model for partial viewing

I want to build a partial view that receives a model column and prints it. Something like that:

In view:

@model IEnumerable<products_comparison.Models.Product> @{ ViewBag.Title = "Index"; var Brand = (from r in Model select r.Brand).Distinct(); } <h2> Index</h2> @Html.RenderPartial("_DisplayAttribute",Brand) 

And in a partial image:

 <table> <tr> <th> Brand </th> </tr> @foreach (var row in Model) { <tr> <td> @Html.DisplayFor(r => row) </td> </tr> } </table> 

There are several issues that I am facing:

  • The compiler does not allow me to send Barnd in a partial view.
  • If you look at the partial view code, you will see the word Brand, which is the name of the column. I don’t want to hardcode the word β€œBrand” in a partial representation, instead I like that the column name will be there.
  • In a partial view, I need to put @model products_comparison.Models.Product, but I do not want to send a hole table. I want to send only one column. But I do not know what to put there.

Thanks!

EDIT:

Just to take away one thing, I want the view to invoke the same partial view for each column in the table (for most columns in the table) and every time I send another column (great value for the column, to be precise).

+8
asp.net-mvc razor partial-views ado.net-entity-data-model
source share
2 answers

Start with refactoring and put the right logic in the right place. This LINQ query has nothing to do with the view. The view should not make any LINQ queries or anything else to retrieve data. It is assumed that the view must work with the data that it passes to it from the controller action in the form of a view model. The controller action builds and passes an adapted view model that you define for the view.

So, as always, you start by defining a presentation model that will be adapted to the requirements of your presentation:

 public class MyViewModel { public IEnumerable<Brand> Brands { get; set; } } 

then you write a controller action that populates this view model and passes it to the view:

 public ActionResult Foo() { IEnumerable<products_comparison.Models.Product> products = ... var model = new MyViewModel { Brands = (from r in Model select r.Brand).Distinct() }; return View(model); } 

that view:

 @model MyViewModel <table> <tr> <th> Brand </th> </tr> @Html.DisplayFor(x => x.Brands) </table> 

and finally, you can define an appropriate display template that will be automatically displayed for each element of the Brands collection of your view model ( ~/Views/Shared/DisplayTemplates/Brand.cshtml ):

 @model Brand <tr> <td> @Html.DisplayForModel() </td> </tr> 
+12
source share

For 1, try changing @Html.RenderPartial("_DisplayAttribute",Brand) to @Html.Partial("_DisplayAttribute",Brand)

You also need to specify the model in a partial view, e.g. @model products_comparison.Models.Brand or something like this

Also clarify 2 and 3 as they are not clear what you want.

+7
source share

All Articles