My Quesiton: Within my view model I created a class InterfaceDisplay that houses some display properties and other meta data for setting up my interface entry form.
I’m wondering why when I set a custom id using the additionalViewData object, ex. @Html.EditorFor(item => item.Interface.DependantSystem, new { @id = "ls" }) is my control id still set to the auto generated id="Interface_DependantSystem" I’ve overwritten ids from properties straight from the ViewModel but when they are properties within an object no dice.
My View Model:
public class ApplicationViewModel
{
//Other Properties
public class InterfaceDisplay {
[DisplayName("Linked System")]
public string DependantSystem { get; set; }
[DisplayName("Data Flow")]
public string DataFlow { get; set; }
[DisplayName("Method")]
public string Type { get; set; }
[DisplayName("Name")]
public string Name { get; set; }
[DisplayName("Description")]
[DataType(DataType.MultilineText)]
public string Description { get; set; }
}
}
My View
@model Namespaces.ApplicationViewModel
<table class="content-table">
<tr>
<td style="width: 240px;">
@Html.Hidden("appid", @Model.Id)
@Html.LabelFor(item => item.Interface.DependantSystem)
@Html.EditorFor(item => item.Interface.DependantSystem, new { @id = "ls" })
@Html.LabelFor(item => item.Interface.DataFlow)
@Html.EditorFor(item => item.Interface.DataFlow, new { @id = "df" })
@Html.LabelFor(item => item.Interface.Type)
@Html.EditorFor(item => item.Interface.Type, new { @id = "mt" })
@Html.LabelFor(item => item.Interface.Name)
@Html.EditorFor(item => item.Interface.Name, new { @id = "nm" })
@Html.LabelFor(item => item.Interface.Description)
@Html.EditorFor(item => item.Interface.Description, new { @id = "de" })
</td>
</tr>
</table>
The second argument that you are passing to the
EditorForhelper is defining an additional view data for the corresponding editor template. But if you don’t write a custom editor template, then thedefault templatewill be used. And as you can see from the blog post I’ve linked to in my previous sentence, this default template does absolutely nothing with the additional view data property argument that you are passing to it.So if you want this parameter to have some effect you will have to write a custom template – an editor template in your case, because you are using the EditorFor helper. Since you are applying it to a property of a string type (
Description) you could add the following custom editor template~/Views/Shared/EditorTemplates/string.cshtmlwith the following contents:Notice how I have overriden the default template and passed the ViewData as third argument to the TextBox helper so that the anonymous object you created in your view as second argument to the EditorFor helper ends up as HTML attributes of the corresponding input field that this template will generate.
Now since you have overriden the default editor template for the string type whenever you use
Html.EditorFor(x => x.SomePropertyOfTypeString, new { @class = "foo", id = "bar" }), this custom template will be used instead of the default one and it will generate the following markup for you: