Alex Rivera | Logout

MVC DropDownListFor basic True False in View

Asked 2011-10-06T01:07:27.860
18

I'm trying to setup a basic DropDownListFor in MVC:

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

This is in my view and what I'm wanting is a basic dropdown of true and false with values of 1 and 0 respectively.

I'm thinking the part I have wrong is adding the items to the SelectList constructor.

Can somebody help me with this?

Edit
Report

1 Answer

3

For something like this, why don't you simply just emit a 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've had to do a few of these, and I actually wrote this as an extension method to HtmlHelper, but its a lot cleaner, its easy to debug and it's faster for the site in general.

answered 2011-10-06T01:13:50.893

Your Answer