Link to home
Start Free TrialLog in
Avatar of monica73174
monica73174

asked on

dropdownlistfor not selecting value

I have a dropdown list in an MVC 5 razor view.  My model is passing through the correct value but the value is never selected in the list.  I am not sure what else I can do?

Model
            [DisplayName("State/Province")]
            public Nullable<int> state_province_id { get; set; }

Open in new window


View
                        <div class="form-group">
                            @Html.LabelFor(m => addy.state_province_id, new { @class = "control-label col-md-3" })
                            @Html.DropDownListFor(m => addy.state_province_id, item.state_province_dropdown, "-- Select --", new { @class = "form-control" })
                        </div>

Open in new window

Avatar of Lokesh B R
Lokesh B R
Flag of India image

Hi,

try this

 @Html.DropDownListFor(m => addy.state_province_id, item.state_province_dropdown.Value, "-- Select --", new { @class = "form-control" })

Open in new window

Avatar of monica73174
monica73174

ASKER

Thanks for the suggestion but that just throws an error in Visual Studio.
Check whether it has value or not.

 @Html.DropDownListFor(m => addy.state_province_id, item.state_province_dropdown.HasValue ? item.state_province_dropdown.Value : null, "-- Select --", new { @class = "form-control" })

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of monica73174
monica73174

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial