3

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 ??

4

1 回答 1

4

表单字段不是按 ID 提交的,而是按名称提交的。可以有两个具有相同名称的控件。但是,具有相同 id 的两个元素是无效的 HTML。

您可以设置不同的 ID,但保持相同的名称,如下所示:

@Html.HiddenFor(x => x.DepartmentID, new { id="DepartmentID2" })
于 2013-06-03T22:31:31.390 回答