MVC DropDownListFor basic True False in view

I am trying to set the base DropDownListFor in MVC:

@Html.DropDownListFor(modelItem => item.CheckerApproved, new SelectList(new SelectListItem { Text = "True", Value="1" } , new SelectListItem { Text = "False", Value="0"})) 

This, in my opinion, and what I want is a basic disclosure of true and false with values 1 and 0 respectively.

I think that I have incorrectly adding elements to the SelectList constructor.

Can someone help me with this?

+10
source share
4 answers

Try the following:

 @Html.DropDownListFor(modelItem => modelItem.CheckerApproved, new [] { new SelectListItem { Text = "True", Value="1" } , new SelectListItem { Text = "False", Value="0"} }) 
+27
source

Something like this, why don't you just just release the Select tag with options in your view?

 <select id='ddlTrueFalse' name='ddlTrueFalse'> <option value='1'>True</option> <option value='0'>False</option> </select> 

Then in your action add the parameter:

 public ActionResult MyAction(string ddlTrueFalse) { //ddlTrueFalse will be "1" or "0" } 

I had to make some of them, and I actually wrote this as an extension method for HtmlHelper, but it is much cleaner, it is easy to debug and faster for the site as a whole.

+3
source

This already exists - if you do Html.EditorFor(model => model.MyBoolean) , you will get a drop-down list with True / False and default to Unset or similar.

+2
source

It was difficult for me to understand how to add a β€œclass” to the code above, so I shared this, I had to put the original drop-down list in brackets, and then add an overload

 @Html.DropDownListFor(modelItem => modelItem.CheckerApproved, (new[] { new SelectListItem { Text = "True", Value = "1" }, new SelectListItem { Text = "False", Value = "0" } }), new { @class = "form-control" } ) 
-one
source

All Articles