MVC 3 — inside a foreach loop, avoiding duplicate html?

Up vote 1 down vote favorite share g+ share fb share tw.

I have an mvc view that uses "tabs", each of these tabs displays a different form of the same data (available from my viewmodel). The data is displayed as follows: @foreach (Path p in Model.Paths. Where(r => r.

PathType == "HR")) { (displays html and data) } @foreach (Path p in Model.Paths. Where(r => r. PathType == "IT")) { (displays html and data) } and so on...since each iteration uses the same html and named variable (p), what is the best way of not repeating these values?

Asp.net asp. Net-mvc asp. Net-mvc-3 razor link|improve this question asked Aug 21 '11 at 20:53db.

Max1535 100% accept rate.

Use a custom display template. Shared\DisplayTemplates\Path. Cshtml @model YourApp.Model.

Path @* (displays html and data) *@ MainView. Cshtml @Html. DisplayFor(model => model.

HrPaths) @Html. DisplayFor(model => model. ItPaths)} You should split out the Paths object in your VM into seperate properties, e.

G put the . Where filter into the Controller. That way, you only have a single template to render out the type (since it's the same type for both properties), you avoid foreach loops, you maintain the HTML in one place, etc.\ You could also go one step further and create a Dictionary> in your View Model, where the key is the tab name, the the value is the pre-filtered collection of Path.

Then your View simply becomes: @Html.DisplayForModel() Then the outer template (you will need a UIHint on the VM): @Html. DisplayFor(model => model. Value).

Make the loop include the tabs, something like this: @var i=0; @foreach (string type in Model. PathTypes) { @i++; @foreach (Path p in Model.Paths. Where(r => r.

PathType == type)) { (displays html and data) } } I don't know the MVC 3 client-side syntax, but you get the idea.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions