How to Select default value in dropdown in mvc base on conditionAniket Narvankar5yAsked Apr 22, 2021, 5:29 AM836Learn iOS Programming 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 4 RepliesKnow the answer? Post it — somebody with the same question will find it here.Sign in to answer this questionIt is the same account you read, post and publish with — and you will come straight back to this page.Sign inCreate an accountJignesh Kumar5y agoPosted Apr 23, 2021, 10:15 AMAccepted answerPlease 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 } } +1Aniket Narvankar5y agoPosted Apr 23, 2021, 8:56 AM 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 +1Asma Khalid5y agoPosted Apr 22, 2021, 7:06 AMLook into this i.e. http://bit.ly/2fzMWjE Mark answer as accepted if helpful Thanks & Regards,Asma Khalid +1Jignesh Kumar5y agoPosted Apr 22, 2021, 6:57 AMHi, Please use this link,https://stackoverflow.com/questions/34624034/select-tag-helper-in-asp-net-core-mvc +1
Jignesh Kumar5y agoPosted Apr 23, 2021, 10:15 AMAccepted answerPlease 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
Aniket Narvankar5y agoPosted Apr 23, 2021, 8:56 AM 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 +1Asma Khalid5y agoPosted Apr 22, 2021, 7:06 AMLook into this i.e. http://bit.ly/2fzMWjE Mark answer as accepted if helpful Thanks & Regards,Asma Khalid +1Jignesh Kumar5y agoPosted Apr 22, 2021, 6:57 AMHi, Please use this link,https://stackoverflow.com/questions/34624034/select-tag-helper-in-asp-net-core-mvc +1
Asma Khalid5y agoPosted Apr 22, 2021, 7:06 AMLook into this i.e. http://bit.ly/2fzMWjE Mark answer as accepted if helpful Thanks & Regards,Asma Khalid +1
Jignesh Kumar5y agoPosted Apr 22, 2021, 6:57 AMHi, Please use this link,https://stackoverflow.com/questions/34624034/select-tag-helper-in-asp-net-core-mvc +1