Alka Tomar
Alka Tomar

Reputation: 17

How to fetch selected data's value from disabled list in MVC C#

I have designed a razor-view that is bounded with model. This page containing multiple lists(drop-down). In some case depended child drop-down list is disabled. There is need to fetch it's value. Please help me for it.

Upvotes: 0

Views: 67

Answers (2)

Manveer Singh
Manveer Singh

Reputation: 357

There is need to include a hidden field with the same name and value

@Html.DropDownListFor(x => x.StateId, Model.States, new { disabled = "disabled" })
@Html.HiddenFor(x => x.StateId)
  1. When form is posted with MVC model then respective selected drop-down value will be sent with it.
  2. In case you are posting the form with ajax post then- If drop-down is disabled dynamically with javascript then assign the currently selected value of the dropdown to the hidden field just after disabling it.

Upvotes: 0

AUSteve
AUSteve

Reputation: 3248

Disabled controls will not post their values back to the server-side, see values of disabled inputs will not be submited? for extra info.

If you want to access the value you will have to store (copy) it somewhere else, such as an extra <input type="hidden"/>

Upvotes: 2

Related Questions