Dropdownlistfor will not show the correct selection

前端 未结 2 921
执念已碎
执念已碎 2020-12-02 01:49

I have three dropdownlistfor in a loop that do not show the correct value from the DB. They always default to the first entry. I have checked and double checked the DB and

相关标签:
2条回答
  • 2020-12-02 02:01

    Unfortunately @Html.DropDownListFor() behaves a little differently than other helpers when rendering controls in a loop. For a single object

    @Html.DropDownListFor(m => oProfile.BodyTypeShoulderId, new SelectList(Model.BodyTypeShoulders, "Id", "Name"))
    

    would work fine (if the value of BodyTypeShoulderId matches the value of one of the options, then that option would be selected). Ehsan has shown a work around when using it in a loop, however you have a few other issues in you code, not the least is that many of your properties will not post back correctly to a collection because your using a foreach loop rather than a for loop (which is generating duplicate name and id attributes). Your also generating a new SelectList in each iteration of the loop which is not very efficient.

    You can solve both these and the dropdown selection issue by using an EditorTemplate. Assuming property CustomerMeasurementProfiles is typeof CustomerMeasurementProfiles, then

    CustomerMeasurementProfiles.cshtml (add this to Views/Shared/EditorTemplates or Views/YourController/EditorTemplates)

    @model CustomerMeasurementProfiles
    @Html.HiddenFor(m => m.ID)
    @Html.HiddenFor(m => m.CustomerId)
    ....
    @Html.DropDownListFor(m => m.BodyTypeShoulderId, (SelectList)ViewData["shoulders"])
    ....
    

    In the view model, add properties for the SelectList's and filtered collection of the items to display

    IEnumerable<CustomerMeasurementProfiles> ActiveCustomerMeasurementProfiles { get; set; }
    public SelectList BodyTypeShoulderList { get; set; }
    ....
    

    and assign those values in the controller

    Then in the main view

    @using (Html.BeginForm())
    {
      ...
      @Html.EditorFor(m => m.ActiveCustomerMeasurementProfiles, new { shoulders = Model.BodyTypeShoulderList })
      ...
    

    Using @Html.EditorFor() with a collection will correctly name the controls (and correctly select the right options) and on post back, ActiveCustomerMeasurementProfiles will now be correctly populated with all its properties.

    0 讨论(0)
  • 2020-12-02 02:23

    If you want to set the selected value that is coming in Model. You need to do it like this:

    @Html.DropDownListFor(m => oProfile.BodyTypeShoulderId, 
                               new SelectList(Model.BodyTypeShoulders, 
                                              "Id", 
                                              "Name",
                                              oProfile.BodyTypeShoulderId), 
                               new { @class = "form-control input-sm-select" })
    

    The above code will set the dropdown selected value to whatever is in the current Model object BodyTypeShoulderId

    The first argument of DropDownListFor tells that on form post drop down selected value will be mapped with the Model property which is set there (we are passing m => oProfile.BodyTypeShoulderId) but this not sets selected Value.

    For setting selected value you have to pass SelectList fourth parameter using this overload of SelectList class which is object selectedValue

    0 讨论(0)
提交回复
热议问题