asp.net - How to use a radio button to select a list item in MVC3 -
asp.net - How to use a radio button to select a list item in MVC3 -
i need nowadays user list of bundle options, select one. don't want utilize radio button list, need complex templating each list item. forgive me, can't seem figure out how link column of radio buttons selection property in view model.
my view model has selectedpackageid
(int), , list of memberpackagelistitem
view models represent individual packages. memberpackagelistitem
has packageid
(int), need couple packageid
of selected item selectedpackageid
of root view model.
it hard me post code, inheritance etc. obscures much of want see, i'm hoping outline of mutual scenario, , general guidelines on using radio buttons in scenario suffice help me continue.
i suggest using htmlhelper render radio button list, follows:
[suppressmessage("microsoft.design", "ca1006:donotnestgenerictypesinmembersignatures", justification = "this appropriate nesting of generic types")] public static mvchtmlstring radiobuttonlistfor<tmodel, tlist, tselecteditem>( htmlhelper<tmodel> htmlhelper, expression<func<tmodel, tlist>> expression, expression<func<tmodel, tselecteditem>> selecteditem) { homecoming radiobuttonlistfor(htmlhelper, expression, selecteditem, null /* htmlattributes */); } [suppressmessage("microsoft.design", "ca1006:donotnestgenerictypesinmembersignatures", justification = "this appropriate nesting of generic types")] public static mvchtmlstring radiobuttonlistfor<tmodel, tlist, tselecteditem>( htmlhelper<tmodel> htmlhelper, expression<func<tmodel, tlist>> expression, expression<func<tmodel, tselecteditem>> selecteditem, object htmlattributes) { homecoming radiobuttonlistfor(htmlhelper, expression, selecteditem, htmlhelper.anonymousobjecttohtmlattributes(htmlattributes)); } [suppressmessage("microsoft.design", "ca1006:donotnestgenerictypesinmembersignatures", justification = "this appropriate nesting of generic types")] public static mvchtmlstring radiobuttonlistfor<tmodel, tlist, tselecteditem>( htmlhelper<tmodel> htmlhelper, expression<func<tmodel, tlist>> expression, expression<func<tmodel, tselecteditem>> selecteditem, idictionary<string, object> htmlattributes) { if (expression == null) { throw new argumentnullexception("expression"); } modelmetadata metadata = modelmetadata.fromlambdaexpression(expression, htmlhelper.viewdata); ienumerable<selectlistitem> items = null; if (metadata.model != null) { ienumerable<selectlistitem> modelitems = (ienumerable<selectlistitem>)metadata.model; if (modelitems != null) { items = modelitems; } } modelmetadata selecteditemmetadata = modelmetadata.fromlambdaexpression(selecteditem, htmlhelper.viewdata); homecoming radiobuttonlisthelper(htmlhelper, metadata, selecteditemmetadata, expressionhelper.getexpressiontext(selecteditem), items, htmlattributes); } private static mvchtmlstring radiobuttonlisthelper(htmlhelper htmlhelper, modelmetadata metadata, modelmetadata selecteditemmetadata, string name, ienumerable<selectlistitem> selectlist, idictionary<string, object> htmlattributes) { // verify arguments if (string.isnullorempty(name)) throw new argumentnullexception("name", "name cannot null"); if (selectlist == null) throw new argumentnullexception("selectlist", "select list cannot null"); if (selectlist.count() < 1) throw new argumentexception("select list must contain @ to the lowest degree 1 value", "selectlist"); string fullname = htmlhelper.viewcontext.viewdata.templateinfo.getfullhtmlfieldname(name); string fullid = htmlhelper.viewdata.templateinfo.htmlfieldprefix + "_" + name; idictionary<string, object> validationattributes = htmlhelper .getunobtrusivevalidationattributes(expressionhelper.getexpressiontext(name), selecteditemmetadata); // define items stringbuilder items = new stringbuilder(); // loop through items int32 index = 0; foreach (selectlistitem in selectlist) { // define check box input tagbuilder input = new tagbuilder("input"); input.mergeattribute("type", "radio"); input.mergeattribute("name", fullname, true); if (i.selected) input.mergeattribute("checked", "checked"); input.mergeattribute("value", i.value); if (index == 0) input.mergeattributes(validationattributes); input.mergeattributes(htmlattributes); // define label tagbuilder label = new tagbuilder("label"); label.mergeattribute("for", fullid + "[" + index.tostring() + "].selected"); label.innerhtml = i.text; // add together item items.appendformat("\r\t<div>\r\t\t{0}\r\t\t{1}\r\t</div>", input.tostring(tagrendermode.normal), label.tostring(tagrendermode.normal)); index++; } // homecoming list homecoming new mvchtmlstring(items.tostring() + "\r"); }
please note memberpackagelistitem
must of type ienumerable<selectlistitem>
. usage follows (razor syntax):
@html.radiobuttonlistfor(m => m.memberpackagelistitem, m => m.selectedpackageid)
counsellorben
asp.net asp.net-mvc asp.net-mvc-3 razor
Comments
Post a Comment