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 ではなく、名前で送信されます。同じ名前の 2 つのコントロールがあっても問題ありません。ただし、同じ ID を持つ 2 つの要素を持つことは無効な HTML です。

別の ID を設定できますが、次のように同じ名前を保持できます。

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