ASP.NET MVC RadioButtonListFor is always preset -
i have radiobuttonlistfor custom adapter working, if users form info reset, , no info has been submitted, 1 of radio buttons (the first) preselected, want avoid this, how can accomplish this?
@html.radiobuttonforselectlist(model => model.viewmodelforthingcreate.thingtypeid, model.viewmodelforcarcreate.cartypeselectlist)
and:
public static mvchtmlstring radiobuttonforselectlist<tmodel, tproperty>(this htmlhelper<tmodel> htmlhelper,expression<func<tmodel, tproperty>> expression, ienumerable<selectlistitem> listofvalues) { var metadata = modelmetadata.fromlambdaexpression(expression, htmlhelper.viewdata); var sb = new stringbuilder(); if (listofvalues != null) { foreach (selectlistitem item in listofvalues) { var id = string.format("{0}_{1}", metadata.propertyname, item.value); var radio = htmlhelper.radiobuttonfor(expression, item.value, new { id = id }).tohtmlstring(); sb.appendformat("<label class=\"radio inline\" for=\"{0}\">{1} {2}</label>", id, radio, httputility.htmlencode(item.text)); } } homecoming mvchtmlstring.create(sb.tostring()); }
thanks!
i've tried method in mvc3 template , seems work fine me. i've created model
public class indexmodel { public string id; public ienumerable<selectlistitem> elements; }
then created instance , filled values:
var model = new indexmodel() { id = "a", elements = new list<selectlistitem>() { new selectlistitem() { text = "test1", value = "1"}, new selectlistitem() { text = "test2", value = "2"}} };
in view i've used extension method
<form> @(extensions.radiobuttonforselectlist(html, x => x.id, model.elements)) <button type="reset">reset</button> </form>
all seem fine after launch. fields not selected @ load , they're cleared after pressing "reset" button.
can give more details i'm not sure if understand trying accomplish :-)
edit:
here's illustration in plain html of radio buttons. they're not filled @ origin , if want them required add together required default can send form without selecting radio button. can create 1 checked adding checked in sec example. using javascript on client side? maybe causing side-effect? http://jsbin.com/isadun/1
mz
asp.net-mvc
No comments:
Post a Comment