2
votes

I am passing in a list of objects to my View via a model

@model MyModel.ObjectViewModel

I am new to MVC and am trying to set the initially selected item of a dropdownlist in my view (modelled after an Edit).

I am then binding this to a drop down list as follows

<label for="ddlObjects">Select Object</label>
@Html.DropDownList("ddlObjects", Model.AllObjectsSelectList, Model.Object.ObjectName)

The above does make the drop down list have the correct object selected initially, but I discovered it is only in the form of text. The real object isn't chosen and as such the value isn't used. How can I have a list of items, say, "Object1" "Object2", etc and have the default be a specific one?

When I'm passing through the item I only know the text value (the name that appears in the drop down list) of the item, I don't know it's inner value so I can't really use SelectListItem {Text = "X", Value= "Y"}

I have searched here and through google, and there are options for setting the intially selected value, however they are using methods like @Html.DropDownList for which doesn't seem to let me specify a control name, and in my controller I specifically reference the name of the control.

1
That kind of works in that it says it should set the default to X but it doesn't seem to change it when I actually run the app and go to the page. (second link).MattR
Why not use Viewbag or ViewData to carry your selected id(value) to view and assign that value to selected value overload for dropdown , So your selected id(value) could be dynamic.Suraj Singh
Because I don't have the ID, I only have the text value. I'm dealing with data I get from SQL and instead of storing the ID and using Joins to normalise the table it is just passing the string name of the object.MattR
Try to pass your TextField and Valuefield both as the name Something like this SelectListItem {Text = "X", Value= "X"} .Suraj Singh

1 Answers

2
votes

My work around wasn't pretty, but it could be easily refactored to be much nicer. It will just be a case of some effort which at present I don't have time for - but I will in the next week or so.

I created a method in my controller which I pass in my list of items (selectList, which you would think would work anyway... but it doesn't) then I work out which is the object I require and set the Selected property to true.

private List<SelectListItem> GetListOfObjectTypes(SelectList selectList, string objectTypeName, string objectTypeId)
{
    List<SelectListItem> items = new List<SelectListItem>();
    foreach (METAObjectType item in selectList.Items)
    {
         bool isSelected = false;
         if (item.Name == objectTypeName)
         {
                isSelected = true;
         }
         items.Add(new SelectListItem {Selected= isSelected, Text=item.Name, Value=item.ObjectTypeId.ToString()});
        }
        return items;
    }

I then just pass this through to my View and set it as the list in the @Html.DropDownList and it will now select the correct item by default.