You can create a dropdownlist using
@Html.DropDownListFor(model => model.SelectedId, model.DropDownItems);
but where does DropDownListFor get the value to pass into the model => model.SelectedId lambda from?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
SelectedId is just the integer of the field in the model.
the model is passed in from the controller using
And the model can be defined in the view at the top
So the dropdownbox’s value is set as the
SelectedIdfield in your model.A proper field name for example would be
RoomNoif that clarifies it better.As long as
model.DropDownItems(ormodel.Roomsin my example) Contains a item with the value of that attribute, it will set it as the selected item in the list as default.Edit:
If you are using viewbag, rather than the model, instead use
DropDownList. (each input has aforextension for use with models)This above, creates an input form with id
RoomNo,and uses a
SelectListthat will default to empty if null. The fields that defined the values and text shown are set at the end of the select list parameters.The last bit sets the default value to the contents of
@ViewBag.RoomNo.You can also create the select list in the controller, and then simply set the dropdown options to the
viewbag.myselectListentity.