ASP .NET MVC: Dealing with radio buttons to and from View<->Controller
I have been trying to get simple YES/NO radio buttons list working but facing strange issues. Please refer my following code snippets.
Model:
public bool isTimer { get; set; }
View:
<div class="timer-selection">
<%: Html.Label("Timer") %>
<%= Html.RadioButton("isTimer", Model.isTimer) %> Yes
<%= Html.RadioButton("isTimer", !Model.isTimer) %> No
</div>
Javascript snippet to set isTimer value:
var timer = $(this).children("input.isTimer").val();
$('#UpdateButton_isTimer').val(timer);
Controller:
public virtual ActionResult CustomViewAdd(TimerViewModel model)
{
UITimerTable timer = new UITimerTabl开发者_开发百科e()
{
// My other properties
isTimer = model.NewIsTimer
};
if (ValidateAndSubmitChanges())
return RedirectToAction(MVC.Screen.CustomView(view.Id, false));
return View(model);
}
This adds isTimer value in my database successfully but two things I am not able to understand.
When I select YES, it inserts False value in database and when I select NO, it inserts TRUE! I can exchange NOT(!) operator in View like below but logically it will be wrong.
<%= Html.RadioButton("isTimer", !Model.isTimer) %> Yes <%= Html.RadioButton("isTimer", Model.isTimer) %> No
Though I have written JAVASCript to set the value back when my form again loaded(comes visible), it always returns radio button value as TRUE after I set it to True and whenever it loads, the YES radio buttons gets checked. I never get NO radio button checked! Following is the HTML code I got when inspect element from Chrom! Don't know why YES radio button is getting set always.
<div class="timer-selection"> <label for="Timer">Timer</label> <input checked="checked" id="UpdateButton_isTimer" name="UpdateButton.isTimer" type="radio" value="True"> Yes <input id="UpdateButton_isTimer" name="UpdateButton.isTimer" type="radio" value="True"> No </div>
Any help would be really appreciated!
Thanks.
IT seems like you should just be setting your radio buttons like this
<%= Html.RadioButton("isTimer", True) %> Yes
<%= Html.RadioButton("isTimer", False) %> No
The current state of isTimer doesn't matter. You're trying to set it; not toggle it. If you were going to create a button to toggle it you could use the !Model.isTimer and it would make sense.
To make your radio button check whichever should be checked you need to do this:
<%= Html.RadioButton("isTimer", True, Model.isTimer) %> Yes
<%= Html.RadioButton("isTimer", False, !Model.isTimer) %> No
I believe the third argument is the whether it will be checked or not. So lets look at what the above does. If the Model.isTimer is true then the first radio button's check value is set to true and the second's is set to !true (false). In the other case where Model.isTimer is false the first radio button check value is set to false and the second's is set to !false (true).
Hope that clears things up.
also you might consider set value as htmlAttribute
eg
<%= Html.RadioButton("isTimer",new{Value="true", checked="checked"})%>
this is other way than m4tt1mus
hope this helps
to find out more check out this link http://www.htmlcodetutorial.com/forms/_INPUT_TYPE_RADIO.html
精彩评论