Aniket Narvankar

Aniket Narvankar

  • 833
  • 1.8k
  • 374.6k

How to Select default value in dropdown in mvc base on condition

Apr 22 2021 5:29 AM
<select type="text" name="ErrorId" id="ErrorId_@Model.ICSDocNumber"
onchange="if(ValidateCurrentRowError(this)) calculateQAScore('@Model.ICSDocNumber');">
<option>-select-</option>
@{
if( ViewBag.ErrorTypeMaster!=null){
foreach(var el in ViewBag.ErrorTypeMaster){
<option value="@el.Id" selected="@(el.Id==entity.ErrorId)"
data-error-category="@el.ErrorCategory"> @el.ErrorType </option>
}
}
}
</select>
 
I have a dropdown,in ViewBag.ErrorTypeMaster I have table in this format
Id ErrorId ErrorCategory ErrorType
1  10        Critical             A
2  11        Critical             B
3  12        Critical            C
4  13        Critical             D
 
When a page loads I am getting a value in one varriable that is departmentId,if departmentId is 7 I want by default D Errortype to be selected in drodpown. Kindly guide me on this. What changes I must do while binding dropdown 

Answers (4)