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.