0

I am trying to have all the models used for specific searches into one Generic search view.

I need to render only some of the fields in the model, either with something similar to this : (Psudo code)

foreach (string  textBoxFieldName in  TextBoxFieldNames)
    {
      Html.Toolbox_TextBoxFor(m => m.textBoxFieldName)
    }

or having attributes on the model and checking when that attribute applies e.g. in the model I'll have something like this :

[AppliedCases("Case1","Case4","Case77")]
[ControlToUse("TextBoxFor")]
public string LastName { get; set; }

And some how in the view will be able to go trough all the properties check that if the CurrentCase is one of the AppliedCases of the Model property and if so then use the Razor magic display it accordingly

My question is this correct approach and if so how do I implement it, looking for some examples with dynamic rendering (if that is the name of this topic)

1 Answer 1

1

You could use the overload of Html.Editor that takes the name (as string) of the property you want to render:

var fieldNames = new string[] {"LastName"};
foreach (string fieldName in fieldNames) {
    @Html.Editor(fieldName)
}

You can use the UIHint attribute in the ViewModel to influence which editor template shall be used:

[UIHint("MySpecialStringEditor")]
public string LastName { get; set; }

At the shop where I work, we do not use this approach. Rather, we use different explicit Views of the same ViewModel. This allows for more flexibility and is easier to understand and maintain.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.