Answered by:
How to set the radio button through viewbag in this way?

Question
-
User-1651604128 posted
In my asp.net razor view, I have this radio button codes, this is the Edit razor view, so when the razor is opened, I want the radio button selected based on the ViewBag returns from controller, from the debug, I can see the ViewBag returns value properly
<div class="radio1" style="display: inline-block; white-space: nowrap;"> <input id="select1" disabled type="radio" name="IN_TYPE" value="1" checked = @(ViewBag.INType =="1") >US <input id="select2" disabled type="radio" name="IN_TYPE" value="2" checked = "@(ViewBag.INType == "2")">Canada <input id="select3" disabled type="radio" name="IN_TYPE" value="3" checked = "@(ViewBag.INType == "3")">France <input id="select4" disabled type="radio" name="IN_TYPE" value="4" checked = "@(ViewBag.INType == "4")">International </div>
But I got run time error:
An exception of type 'Microsoft.CSharp.RuntimeBinder.RuntimeBinderException' occurred in System.Core.dll but was not handled in user code Additional information: Operator '==' cannot be applied to operands of type 'System.Collections.Generic.List<System.Web.Mvc.SelectListItem>' and 'string'
and the above error highlighted with the line code of "<input id="select1" disabled type="radio" name="IN_TYPE" value="1" checked = @(ViewBag.INType =="1") >US "
It seems this code "@(ViewBag.INType =="1") does not compile properly,
any idea how to fix it, much appreciated,
Tuesday, July 2, 2019 6:36 PM
Answers
-
User-1651604128 posted
Hi Yuki,
thanks a lot for your help, I tried your codes, unfortunately it shows error of "Items" can not be found.
but when I tried your codes, I found the problem is that I used the wrong viewbag, the ViewBag.INType is the list of items (no sure why I can not identify the Items), I should use another viewbag.PRO_Type which is the specific string item
this is the working code <input id="select1" disabled type="radio" name="IN_TYPE" value="1" checked = @(ViewBag.PRO_TYPE == "1") >US
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, July 3, 2019 12:18 PM
All replies
-
User475983607 posted
The error indicates you set the ViewBag.INTType to System.Collections.Generic.List<System.Web.Mvc.SelectListItem> type most likely in the Action. That bit of code is not shared. For some reason you are trying to compare a List<System.Web.Mvc.SelectListItem> to a string in the View.
IMHO, you need to organize your thoughts and rethink the approach.
Lastly, I recommend using the RadioButtonFor extension as it will automatically handle the selection.
Tuesday, July 2, 2019 7:17 PM -
User1520731567 posted
Hi Peter Cong,
Additional information: Operator '==' cannot be applied to operands of type 'System.Collections.Generic.List<System.Web.Mvc.SelectListItem>' and 'string'
According to your error message,it means that different types can't assign values to each other.
You could modify like: (If you make sure the number of radio buttons)
<div class="radio1" style="display: inline-block; white-space: nowrap;"> <input id="select1" disabled type="radio" name="IN_TYPE" value="1" checked=@(ViewBag.INType.Items[0].fieldName== "1")>US <input id="select2" disabled type="radio" name="IN_TYPE" value="2" checked="@(ViewBag.INType.Items[1].fieldName== "2")">Canada <input id="select3" disabled type="radio" name="IN_TYPE" value="3" checked="@(ViewBag.INType.Items[2].fieldName== "3")">France <input id="select4" disabled type="radio" name="IN_TYPE" value="4" checked="@(ViewBag.INType.Items[3].fieldName== "4")">International </div>
Best Regards.
Yuki Tao
Wednesday, July 3, 2019 5:58 AM -
User-1651604128 posted
Hi Yuki,
thanks a lot for your help, I tried your codes, unfortunately it shows error of "Items" can not be found.
but when I tried your codes, I found the problem is that I used the wrong viewbag, the ViewBag.INType is the list of items (no sure why I can not identify the Items), I should use another viewbag.PRO_Type which is the specific string item
this is the working code <input id="select1" disabled type="radio" name="IN_TYPE" value="1" checked = @(ViewBag.PRO_TYPE == "1") >US
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, July 3, 2019 12:18 PM