views:

87

answers:

2

I use a dropdownlist in one of my create.aspx but it some how doesnt seem to work...

public IEnumerable<SelectListItem> FindAllMeasurements()
    {
        var mesurements = from mt in db.MeasurementTypes
                          select new SelectListItem
                          {
                             Value = mt.Id.ToString(),
                             Text= mt.Name
                          };
        return mesurements;
    }

and my controller,

 public ActionResult Create()
    {
      var mesurementTypes = consRepository.FindAllMeasurements().AsEnumerable();
     ViewData["MeasurementType"] = new SelectList(mesurementTypes,"Id","Name");
     return View();
    } 

and my create.aspx has this,

<p>
  <label for="MeasurementTypeId">MeasurementType:</label>
    <%= Html.DropDownList("MeasurementType")%>
     <%= Html.ValidationMessage("MeasurementTypeId", "*") %>
   </p>

When i execute this i got these errors,

DataBinding: 'System.Web.Mvc.SelectListItem' does not contain a 
 property with the name 'Id'.
A: 

As the error message implies, you need an IEnumerable<SelectList>, not an IEnumerable<Materials>.

The constructor for SelectList has an overload that takes an IEnumerable. See http://stackoverflow.com/questions/1615583/net-mvc-selectlists-and-linq

Robert Harvey
@Robert look at my edit...
Pandiya Chendur
@Pandiya: Well, it's a completely different question now. I see that you found `SelectList`.
Robert Harvey
+2  A: 

In your controller you are creating a new SelectList from IEnumerable<SelectListItem> which is not correct because you've already specified the Value and Text properties.

You have two options:

public ActionResult Create()
{
    var mesurementTypes = consRepository.FindAllMeasurements();
    ViewData["MeasurementType"] = mesurementTypes;
    return View();
}

or:

public ActionResult Create()
{
    ViewData["MeasurementType"] = new SelectList(db.MeasurementTypes, "Id", "Name");
    return View();
}

There's also a third and preferred way using strongly typed view:

public ActionResult Create()
{
    var measurementTypes = new SelectList(db.MeasurementTypes, "Id", "Name");
    return View(measurementTypes);
}

and in the view:

<%= Html.DropDownList("MeasurementType", Model, "-- Select Value ---") %>
Darin Dimitrov
@Ya darin that worked... How to add "Select" as 0th index in that list?
Pandiya Chendur
+1 Darin :) arg, I was too slow :(
PieterG
@PieterG How to add "Select" as 0th index in that list?
Pandiya Chendur
@Pandiya, you could use the proper extension method to add an optional label: `<%= Html.DropDownList("MeasurementType", "-- Please Select a Value ---")%>`
Darin Dimitrov
@Darin how to set value "0" to that "Select"
Pandiya Chendur
@when i inspected my select `<option value="">Select</option>`... I want to set `0` as value...
Pandiya Chendur
Using standard helpers this is not possible, you will have to insert a dummy element to your list at the beginning.
Darin Dimitrov
@Darin any suggestion how to do that?
Pandiya Chendur