views:

650

answers:

4

I have an edit page with a Html.DropDownList in it....I cant show the dropdownlist value it always shows up with Select instead i want to make the dropdown show an item as selected based on a model value say Model.Mes_Id... Any suggestion how it can be done...

       <p>
            <label for="MeasurementTypeId">MeasurementType:</label>
         <%= Html.DropDownList("MeasurementType", // what should i give here?)%>
            <%= Html.ValidationMessage("MeasurementTypeId", "*") %>
        </p>

EDIT: It has the list items but i want to show a value selected in the edit view...

   public ActionResult Edit(int id)
    {
        var mesurementTypes = consRepository.FindAllMeasurements();
        ViewData["MeasurementType"] = mesurementTypes;
        var material = consRepository.GetMaterial(id);
        return View("Edit", material);
    }

My repository method,

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

Have a look at this blog entry.

http://weblogs.asp.net/ashicmahtab/archive/2009/03/27/asp-net-mvc-html-dropdownlist-and-selected-value.aspx

Basically, you need to convert your mesurementTypes list/enumerable into a SelectList or IEnumerable<SelectListItem>.

I would recommend, if possible, upgrading to ASP.NET MVC2 and using Html.DropDownListFor()

Alastair Pitts
@alastair i use `IEnumerable<SelectListItem>` in my repository class then why can't i able to set...
Pandiya Chendur
A: 

You should be returning a SelectionList which can specify a selected item.

How to create a DropDownList with ASP.NET MVC

griegs
@griegs any suggestion how it can be done...
Pandiya Chendur
A: 

Assuming that Model.Mes_Id contais the selected value, you can do something like this

<%
    var Measurements = new SelectList((IEnumerable)ViewData["MeasurementType"], "Id", "Name", Model.Mes_Id);
    Response.Write(Html.DropDownList("measurement_type", Measurements, "Select"));
%>
GerManson
+2  A: 

Set the selected item when you create the IEnumerable<SelectListItem>.

Personally I would create a specialized viewmodel for the form but going by your code, do something like:

public ActionResult Edit(int id)
{
    //Put this first
    var material = consRepository.GetMaterial(id);
    //pass in your selected item
    var mesurementTypes = consRepository.FindAllMeasurements(material.MeasurementTypeId);
    ViewData["MeasurementType"] = mesurementTypes;

    return View("Edit", material);
}

Then change your repository method to something like:

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

    return mesurements;
}

HTHs,
Charles

Charlino
@Charlino that worked..... It works for edit but how will this method act for add view where i dont want a selected value...
Pandiya Chendur
If you just pass in 0 none of the values should be selected - assuming that you don't have a MeasurementType with an id of 0! :-)
Charlino
@Charlino ya just did that... Cheers man!
Pandiya Chendur