Mako
Mako

Reputation: 1515

Getting Disabled Dropdown values in MVC

I am pretty new to MVC. In my form i have a disabled dropdown control whose value is not getting passed to the Model during submit as it is disabled. I tried using a hidden field like below

@Html.DropDownListFor(model => model.DepartmentID, new List<SelectListItem> { new SelectListItem { Text = "Item 1", Value = "1" }, new SelectListItem { Text = "Item 2", Value = "2", Selected = true } })

@Html.HiddenFor(model => model.DepartmentID)

Both of the above statement produce controls having the same id so i was not sure can i get the value of the dropdown in the hidden field.

I could use a different id while creating the hidden variable and assign the dropdown value it it using jquery.

I just want to know if i can achieve the same using the Hidden field having the same id as shown in the above code ??

Upvotes: 3

Views: 4995

Answers (1)

Erik Funkenbusch
Erik Funkenbusch

Reputation: 93424

Form Fields are not submitted by ID, they're submitted by name. It's ok for there to be two controls with the same name. However, it's invalid HTML to have two elements with the same id.

You can set a different ID but keep the same name like this:

@Html.HiddenFor(x => x.DepartmentID, new { id="DepartmentID2" })

Upvotes: 4

Related Questions