asp.net mvc - MVC 5 EditorTemplate for Model renders with same id and name -
i have parent model "receipt" , has dictonary of objects property called customcontroldictionary of class customcontrolmodel. in view loop throuh each object in customcontroldictionary property , call editortemplate. in template add label , textbox customcontrolmodel object. when code renders html controls have same id , name attributes : id="key_value" name="key.value" . did try using regualr arrays or lists instead of dictionary ,but same result. looking forward advise.
model:
public class receiptmodel { public receiptmodel(){} public int receiptid { get; set; } public string receiptnumber { get; set; } public dictionary<string, customcontrolmodel> customcontroldictionary{get; set; } // public list<customcontrolmodel> customcontrollist { get; set; } } public class customcontrolmodel { public customcontrolmodel(){} public int customcontrolid{ get; set; } public string customcontrolname{ get; set; } public string labelcaption{ get; set; } public string value{ get; set; } } view:
//view (@receiptmodel): @foreach (var key in model.customcontroldictionary ) { @html.editorfor(m => key.value,"customcontrolmodel") } editor template:
@model ewms_mvc.classes.customcontrolmodel @html.hiddenfor(model => model.customcontrolid) @html.labelfor(model => model.customcontrolid, model.labelcaption) @html.textboxfor(model => model.customcontrolid, new { @value = @model.value }) rendered html has same id , name obects in customcontroldictionary property of receiptmodel:
<input id="key_value_customcontrolid" name="key.value.customcontrolid" type="hidden" value="201922" />//value here should "id" of input field <label for="key_value_customcontrolid">receipt number:</label> <input id="key_value_customcontrolid" name="key.value.customcontrolid" type="text" value="201922" />//value here should "id" of input field
i think best approach situation avoid htmlhelper methods in template , use html controls instead:
@model ewms_mvc.classes.customcontrolmodel <label>@model.labelcaption</label> <input id="@model.customcontrolid" name="@model.customcontrolname" type="text" value="@model.value" />
Comments
Post a Comment