How to Select default value in dropdown in mvc base on conditionAniket Narvankar5yAsked Apr 22, 2021, 1:29 AM EDTSolvedShareLinkedInXCopy linkSave onchange="if(ValidateCurrentRowError(this)) calculateQAScore('@Model.ICSDocNumber');"> -select- @{ if( ViewBag.ErrorTypeMaster!=null){ foreach(var el in ViewBag.ErrorTypeMaster){ data-error-category="@el.ErrorCategory"> @el.ErrorType } } } I have a dropdown,in ViewBag.ErrorTypeMaster I have table in this formatId ErrorId ErrorCategory ErrorType1 10 Critical A2 11 Critical B3 12 Critical C4 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
How to Select default value in dropdown in mvc base on conditionAniket Narvankar5yAsked Apr 22, 2021, 1:29 AM EDTSolvedShareLinkedInXCopy linkSave onchange="if(ValidateCurrentRowError(this)) calculateQAScore('@Model.ICSDocNumber');"> -select- @{ if( ViewBag.ErrorTypeMaster!=null){ foreach(var el in ViewBag.ErrorTypeMaster){ data-error-category="@el.ErrorCategory"> @el.ErrorType } } } I have a dropdown,in ViewBag.ErrorTypeMaster I have table in this formatId ErrorId ErrorCategory ErrorType1 10 Critical A2 11 Critical B3 12 Critical C4 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
Jignesh KumarRank #91 · 22.9K reputation5y agoPosted Apr 23, 2021, 6:15 AM EDT✓ Accepted answer+1Please try this one, if( ViewBag.ErrorTypeMaster!=null){ foreach(var el in ViewBag.ErrorTypeMaster){ "@el.Id" @((el.Id == 4 && (int)ViewData["departmentId"] == 7) ? "selected" : "") data-error-category="@el.ErrorCategory">@el.ErrorType } } +1
Jignesh KumarRank #91 · 22.9K reputation5y agoPosted Apr 22, 2021, 2:57 AM EDT+1Hi, Please use this link,https://stackoverflow.com/questions/34624034/select-tag-helper-in-asp-net-core-mvc +1
Asma KhalidRank #160 · 14.7K reputation5y agoPosted Apr 22, 2021, 3:06 AM EDT+1Look into this i.e. http://bit.ly/2fzMWjE Mark answer as accepted if helpful Thanks & Regards,Asma Khalid +1
Aniket NarvankarRank #903 · 2.3K reputation5y agoPosted Apr 23, 2021, 4:56 AM EDT+1 onchange="if(ValidateCurrentRowError(this)) calculateQAScore('@Model.ICSDocNumber');"> -select- @{ if( ViewBag.ErrorTypeMaster!=null){ foreach(var el in ViewBag.ErrorTypeMaster){ @el.ErrorType } } } I am using ternary operator with selected as follows selected='@(el.Id==4 && (int)ViewData["departmentId"]==7)?"selected":null' but selected is getting set to selected even if condition is false or true. What change should I do in condition or how should I use ternary operator with selected. Please help me out on this +1Join to answer this questionEvery answer you publish earns 20 SHARP.Create free accountSign inKnow the answer?Answer a question in the forums and earn +20 SHARP for every answer.See unanswered questionsForumsTop experts01Arun PottiAnswers in the forums20answers02Tahir AnsariAnswers in the forums5answers03Kamlesh KumarAnswers in the forums5answers04Sam HobbsAnswers in the forums4answers05Željko PerićAnswers in the forums3answers?YouYour name goes here.Every answer: 20 SHARP.JoinFull leaderboard →2026DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Jignesh KumarRank #91 · 22.9K reputationPosted Apr 23, 2021, 6:15 AM EDT
data-error-category="@el.ErrorCategory">@el.ErrorType
Jignesh KumarRank #91 · 22.9K reputationPosted Apr 22, 2021, 2:57 AM EDT
Asma KhalidRank #160 · 14.7K reputationPosted Apr 22, 2021, 3:06 AM EDT
Aniket NarvankarRank #903 · 2.3K reputationPosted Apr 23, 2021, 4:56 AM EDT
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questionsTop experts