-->

When dropdownlitFor is disabled , value always pas

2019-08-02 17:37发布

问题:

This is my dropdownlistFor

@Html.DropDownListFor(m => m.objTicket.DepartmentId, new SelectList(Model.objTicket.Departments, "DepartmentId", "Department"), 
"-- Select Department--", new { id = "Deptment"}, disabled="disabled")

How can I pass the value which is already stored in objTicket.DepartmentId ?

If i change remove disabled ="disabled", i get my correct value.

回答1:

Form fields are submitted using Names instead of Ids. Disabled controls values wont be submitted by browsers. Place a @Html.HiddenFor field with same name and different Ids as given below.

@Html.DropDownListFor(m => m.objTicket.DepartmentId, new SelectList(Model.objTicket.Departments, "DepartmentId", "Department"),Department--", new { id = "Deptment"}, disabled="disabled")
@Html.HiddenFor(m => m.objTicket.DepartmentId, new { id="Deptment2" })