Sunday 15 January 2012

How do I create a model-bound Yes/No radio button which has neither option selected on page load in ASP.NET MVC? -


In my thoughts model I have an outstanding bullion property:

  see public class { Required (error message = "please do not select yes or no")] Public Bull? Completed (Receive; Set;}}   

I want to control the form for this property that the radio button with neither selected on the option page load The reason for this is that we have the user deliberately click on one option or another (unlike a checkbox, which is easy to ignore and skip by default).

Boolean property is required to be failed, because otherwise The option 'No' is already checked on page load, as I mentioned above, what we do not want is that. However, try using the following code:

   No & lt; / label & gt;   

is a result of a runtime error because the model properties that help to bind are empty.

I Initially How can I get the selected status?

Try this way:

 < Code> & lt; Label & gt; @ Html.RadioButton ("full", "true") Yes & lt; / Labels & gt; & Lt; Label & gt; @ Html.RadioButton ("full", "wrong") & lt; / Label & gt;   

But since you already have a visual model, it would be better to use a firmly typed version of Assistant:

  & lt ; Label & gt; @ Html.RadioButtonFor (x => x.completed, "true") Yes & lt; / Label & gt; & Lt; Label & gt; @ Html.RadioButtonFor (x => X. Filled, "False") No & lt; / Label & gt;    

No comments:

Post a Comment