I'm trying to create an EditorFor() for a List of a Complex Type. Specifically the "Options" below should get displayed in a one multitext input where each option(string) is in a new line. However, I can only display one option in a textbox and not all options....
My View Model and Class:
public class ItemViewModel
{
public int itemId { get; set; }
[UIHint("Option")]
public List<Option> Options { get; set; }
}
public class Option
{
public string Text { get; set; }
}
My Editor Templates:
EditorTemplates\Item.cshtml
@model ItemViewModel
@Html.EditorFor(model => model.Options)
EditorTemplates\Option.cshtml
//Not sure how to dispay the options here
<textarea rows="4" cols="50">
Display Options
</textarea>
If I update my EditorTemplates to:
EditorTemplates\Item.cshtml
@model ItemViewModel
@Html.EditorFor(model => model.Options[0])
EditorTemplates\Option.cshtml
@Html.TextBoxFor(x => x.OptionText)
It will display the first option in a textbox. But, again what I'm trying to achieve is to display all options in a multitext input.
Any ideas?
TextBoxFor will always show the textbox element no matter which kind of property we are binding it with. But EditorFor is much flexible in this case as it decides which element will be more suitable to show the property.
EditorFor<TModel,TValue>(HtmlHelper<TModel>, Expression<Func<TModel,TValue>>, String, String, Object) Returns an HTML input element for each property in the object that is represented by the expression, using the specified template, HTML field name, and additional view data.
EditorFor does not allow for styling as there are no parameters for additional attributes. The reason for this is because the EditorFor doesn't always generate a single element as it can be overridden. To style a specific type of element you need to use the specific editor you want to use.
Simply put, the Html. EditorFor method allows the developer to retain control over the display of form elements by data type (ie. string, boolean, int…etc) or model attribute at a global level rather than at an individual view level. This allows for cleaner ASP markup and easily scalable form controls.
Just create a view in Shared/EditorTemplates/Option.cshtml
@model Option
@Html.TextBoxFor(m => m.Text)
And call
@Html.EditorFor(model => model.Options)
EditorFor iterates over collection for you.
You nearly have it.
In this EditorTemplates\Option.cshtml
add the following:
@model IEnumerable<Option>
@foreach(var option in Model)
{
@Html.TextBoxFor(m => option.Text)
}
Then call it in your view like this:
@Html.EditorFor(model => model.Options)
If you are not populating your options on the initial get, you will need to add this in your ItemViewModel class:
public class ItemViewModel
{
public ItemViewModel()
{
Options = new List<Option>();
}
public int itemId { get; set; }
[UIHint("Option")]
public List<Option> Options { get; set; }
}
This constructor initializes the collection:
public ItemViewModel()
{
Options = new List<Options>();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With