0

i want to disable DropDownList on edit mode in asp.net mvc razor

i have this code:

 @Html.DropDownListFor(m => m.FK_TypeID, new List<SelectListItem> { new SelectListItem { Text = "Select", Value = "", Selected = true } }, new { @class = "form - control" })

how to achive it

Phoenix
  • 263
  • 1
  • 3
  • 13
  • What is the point of a disabled dropdownlist (and its value will never be submitted), but you can use `@Html.DropDownListFor( ..... new { @class = "form - control", disabled = "disabled" })` –  May 02 '17 at 10:19

2 Answers2

0

Shouldn't be difficult if you know when the view is in edit mode. Please refer below:

@if(isEditMode == true)
{
@Html.DropDownListFor(m => m.FK_TypeID, new List<SelectListItem> { new SelectListItem { Text = "Select", Value = "", Selected = true } }, new { @class = "form - control",@disabled="disabled" })
}
else{
@Html.DropDownListFor(m => m.FK_TypeID, new List<SelectListItem> { new SelectListItem { Text = "Select", Value = "", Selected = true } }, new { @class = "form - control" })
}

Is this what you want to achieve?

User3250
  • 2,961
  • 5
  • 29
  • 61
0

i found a shorter solution using

@Html.DropDownListFor(m => m.ID, new List<SelectListItem> { new SelectListItem { Text = "Select", Value = "", Selected = true } }, @Model.ID>0 ? (object)new { @class = "form - control", @disabled = "disabled" } : (object)new { @class = "form - control" })

with the help of this link Conditional html attribute with Html Helper

Community
  • 1
  • 1
Phoenix
  • 263
  • 1
  • 3
  • 13