asp.net mvc - Refactor Control Pattern in View -
the following code snippet represents items in large form. objective refactor down.
<div class="span3"> <ul style="list-style-type: none"> <li><b>description</b></li> <li> <textarea style="width: 100%;" rows="4">@model.item.description</textarea> </li> </ul> </div> <div class="span3"> <ul style="list-style-type: none"> <li><b>truck</b></li> <li> @html.dropdownlistfor(model => model.item.shippingtruckid, new selectlist(model.shippingtrucks, "id", "truck")) </li> </ul> </div> <div class="span3"> <ul style="list-style-type: none"> <li><b>cost</b></li> <li> <input value="@model.item.cost"/> </li> </ul> </div>
two approaches i've considered partial view , html helper. though, how pair down smaller easier maange segment. i'm either going have same structure either input, text area, drop down, or in cases label. there another, superior, approach haven't thought of, or inherent disadvantages/advantages/challenges 1 i've mentioned?
with reflection can read properties , editorfor create elements:
example reflection i
example reflection ii
with first example:
@foreach (var property in model.enumerateproperties()) { <div class="span3"> <ul style="list-style-type: none"> <li><b>@html.labelforproperty(model,property);</b></li> <li> @html.editorforproperty(model,property); </li> </ul> </div> }
and here or here can see how use editor template diferent type of property or use uihint.
edit comment:: "how handle dropdownlists way?"
in case can think of solution, bit complex. can on property put attribute, like:
[itpropertyhavealist("propertywithlistlist")] public int propertywithlistid { get; set; } public ienumerable<selectlistitem> propertywithlistlist { get; set; }
then extension editorforproperty
can detect if property has attribute like example:
[stringlength(20, minimumlength = 5, errormessage = "first name must between 5 , 20 characters")] public string firstname {get;set;} stringlengthattribute strlenattr = typeof(person).getproperty("firstname").getcustomattributes( typeof(stringlengthattribute), false).cast<stringlengthattribute>().single(); int maxlen = strlenattr.maximumlength;
in attribute put method list data create dropdownlists. see this response see how create template dropdownlists.
Comments
Post a Comment