c# - Best way to display a group/selection of radiobuttons from the choice of a single radiobutton? -
i have form questions ask if included , if isn't supply reason.
so need radio button records database it's value normal have setup radiobuttonfor , if "no"(false) selected group/list of other radiobuttons display.
ofc ideal solution if method isn't feasible solution maybe if statement in controller if main radiobutton has value of "yes"(true) set values of x, y , z radiobuttons "no"(false) when records form database.
these 2 ideas have on how same end result 1st idea think easiest way perform it's function in jquery i'm new @ struggle come how
for 2nd idea it's 1 not ideal , 2 i'm not sure how reference radiobuttons/code if statement said task.
any other ideas welcome on how implement them.
well, may sound overkill, go both solutions. need javascript script side code right presentation standpoint - , need server-side code validation right too.
if implement client-side validation, how system behave if browser has no support javascript, or if disabled? cannot take javascript support granted...
otoh, offer best user experience if added client-side functionality you're talking about...
and doubt of how server-side validation: that's easy asp.net mvc - on load, set same viewdata entry/viewmodel property have read during post.
edit let's talk complete solution.
again, i'm not sure understood need here. you're talking radiobuttons, seem think you'll able control them individually (many radios binding many fields). that's not case - group of radiobuttons bound same field, each radiobutton meaning different value (so single dropdown list). of course, not mean database must behave in same way...
see example:
<% using(html.beginform("handleform", "home")) { %> select favorite color:<br /> <%= html.radiobutton("favcolor", "blue", true, new { id = "rbcolorblue", class = "favcolor" }) %> blue <br /> <%= html.radiobutton("favcolor", "purple", false, new { id = "rbcolorpurple", class = "favcolor" })%> purple <br /> <%= html.radiobutton("favcolor", "red", false, new { id = "rbcolorred", class = "favcolor" })%> red <br /> <%= html.radiobutton("favcolor", "orange", false, new { id = "rbcolororange", class = "favcolor" })%> orange <br /> <%= html.radiobutton("favcolor", "yellow", false, new { id = "rbcoloryellow", class = "favcolor" })%> yellow <br /> <%= html.radiobutton("favcolor", "brown", false, new { id = "rbcolorbrown", class = "favcolor" })%> brown <br /> <%= html.radiobutton("favcolor", "green", false, new { id = "rbcolorgreen", class = "favcolor" })%> green <%= html.radiobutton("favcolor", "other", false, new { id = "rbcolorother", class = "favcolor" })%> other <div id="divothercolortext" style="display: block"> describe color want here:<br /> <%=html.textarea("othercolortext", new { id = "taothercolor" }) %><br /> </div> <% } %>
this bound single controller parameter, favcolor
, default value of "blue". see that, convenience, we're assigning distinct client-side id each radiobutton (rbcolorblue
, rbcolorgreen
, forth). means you'll able treat each radiobutton individually in jquery code, if represent single value server-side controller.
talking server-side code, that's how action like:
public class homecontroller : controller { public actionresult handleform(string favcolor, string othercolortext) { // add action logic here // if want have separated field database, // that: mydbfacade.bluecolorfield = (favcolor == "blue"); mydbfacade.greencolorfield = (favcolor == "green"); ... return view(); } }
(of course, work viewmodel, i'll not talk option here.)
back client-side. let's suppose don't want show taothercolor
, unless user selects rbcolorother
radiobutton. jquery code this:
$(document).ready(function() { // if user selects radiobutton: if ( $('#rbcolorother:checked').length > 0) { $("#divothercolortext").show(); } else { $("#divothercolortext").hide(); } });
i guess it. let me know if have missed something... :-)
Comments
Post a Comment