Conditional view of fields in a view model in razor?

To keep it simple, and avoid complex if logic in your view, just create three different views, with only the data you need in each view. Then select the view in your controller based on the question type.

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

I have the following view model to query my table: QuestionViewModel. Cs public enum TypeQuestion { Long = 1, Short = 2, Small = 3, } public class QuestionViewModel { public string Name { get; set; } public string LastName { get; set; } public string Address { get; set; } public string MaxAge { get; set; } public string Category { get; set; } public string Account { get; set; } public TypeQuestion CurrentTypeQuestion { get; set; } } if the query I'm doing is of type: Long: displays all fields. Short: displays Name, LastName, Address, MaxAge.

Small: displays Name, LastName. Is there any way to put some kind of DataAnnotation to determine which fields to display in the view or some other way? , To avoid putting a "what if?

" for each field. Thank you. C# asp.

Net-mvc-3 razor link|improve this question asked Oct 24 '11 at 20:11andres descalzo4,1421922 93% accept rate.

Ok, but in my real case I have 7 options and I need something to avoid creating 7 views – andres descalzo Oct 24 '11 at 20:20 You either need to have logic in the view to show/not show fields, or you need different views. There is no magic here. – Jared Peless Oct 24 '11 at 20:29 Really, this is the whole point of the MVC pattern.

– BuildStarted Oct 24 '11 at 20:38.

This may be overkill, and i'd in fact lean towards @Mystere Man's answer, but this is another option. Instead of regular primitive types in your ViewModel, set them up to cater for the logic. Looks like Name and LastName are always displayed, whilst Address and MaxAge are conditional.

So, setup your ViewModel like this: public class QuestionViewModel { public string Name { get; set; } public string LastName { get; set; } public IEnumerable ConditionalFields { get; set; } public string Category { get; set; } public string Account { get; set; } } public class ConditionalField { public string Field { get; set; } public bool Display { get; set; } } In your controller, setup the nested viewmodel and the boolean values for Address and MaxAge ccording to the value of CurrentTypeQuestion. Then, have your View like this: /Views/Questions. Cshtml @model QuestionViewModel @Html.DisplayForModel() Then create a custom display template (or editor template, if this is a form) for QuestionViewModel: /Views/DisplayTemplates/QuestionViewModel.

Cshtml @model QuestionViewModel @Html. DisplayFor(model => model. Name) @Html.

DisplayFor(model => model. LastName ) @Html. DisplayFor(model => model.

Category) @Html. DisplayFor(model => model. Account) @Html.

DisplayFor(model => model. ConditionalFields) Then create another custom display template for ConditionalField: Views/DisplayTemplates/ConditionalField. Cshtml @model ConditionalField @if (Model.

Display) { @Html.DisplayForModel() } As I said, may be overkill, but in the end, you only have a single if statement in the custom template, no loops, and your main view and first-level template stays clean.

I see this option bradwilson.typepad.com/blog/2009/10/… – andres descalzo Oct 25 '11 at 14:31.

Based from this link and this link Controller: public ActionResult Consulta() { return View(new QuestionViewModel()); } ViewModel: public enum TypeQuestion { Long = 1, Short = 2, Small = 3, } public class QuestionViewModel { public string Name { get; set; } public string LastName { get; set; } public string Address { get; set; } public int MaxAge { get; set; } public string Category { get; set; } public string Account { get; set; } public TypeQuestion CurrentTypeQuestion { get; set; } public bool EnabledField(ModelMetadata field) { //check pending implementation return true; } } View: @model MySite. QuestionViewModel @using System. Linq; @using System.

Collections; @{ ViewBag. Title = "Question"; Layout = "~/Views/Shared/Layout. Cshtml"; } Question @using (Html.

BeginForm(new { id = "FormQuestion" })) { foreach (var prop in ViewData.ModelMetadata. Properties . Where(pm => pm.

ShowForDisplay &&!ViewData.TemplateInfo. Visited(pm) && ViewData.Model. EnabledField(pm))) { if (prop.

HideSurroundingHtml) { Html. Editor(prop. PropertyName); } else { @(prop.

IsRequired? "*" : "") @Html. Label(prop.

PropertyName) @Html. Editor(prop. PropertyName, prop.

Model) @Html. ValidationMessage(prop. PropertyName, "*") } } }.

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