0

I have the following model:

public class Letters{
    public List<KeyValuePair> RecipientTypeList { get; set; }
    public string RecipientType { get; set; }
}
public KeyValuePair{
    public string Value { get; set; }
    public string Key { get; set; }
    public long ObjectId { get; set; }
    public bool IsActive { get; set; }
    public long? Order { get; set; }
    public bool IsOther { get; set; }
}

In my controller the RecipientTypeList is set as below:

Letters letter = new Letters(); 
List<KeyValuePair> recipientTypeList = new List<KeyValuePair>()
{
    new KeyValuePair {ObjectId = (long)RecipientType.InApp, Value = "InApp", IsActive = true},
    new KeyValuePair {ObjectId = (long)RecipientType.Email, Value = "Email", IsActive = false}
};

letter.RecipientTypeList = recipientTypeList;

In my view, I have the following:

@foreach (var recipientType in Model.RecipientTypeList)
{
    var ids = "list-option-" + recipientType.ObjectId;
    var htmlOptions = recipientType.IsActive ? new { @id = ids, @checked = "checked" } : new { @id = ids, @checked = "none" };
    <li class="mdl-menu__item">
        @Html.RadioButtonFor(model => model.RecipientTypeList, recipientType.ObjectId, htmlOptions)
        @Html.Label(ids, recipientType.Value)  
    </li>
}

I have the radio button to be checked depending on the IsActive value : if true then check the radio button else don't.

However the issue that I am getting that the Email radio button with the IsActive = false is being checked instead of the InApp one.

When I inspect the rendered html, I can see that InApp radio button has the checked attribute to checked and the Email one the checked attribute to none.

I know it is the checked attribute which is setting the last radio button to checked but would like the radio button to be checked depending on the IsActive value.

Any idea of how to do this?

Thanks

refresh
  • 1,319
  • 2
  • 20
  • 71
  • You do not need the `checked` attribute when you want your radiobox unchecked: `var htmlOptions = recipientType.IsActive ? new { @id = ids, @checked = "checked" } : new { @id = ids}` OR just set the checked attribute to empty: `var htmlOptions = recipientType.IsActive ? new { @id = ids, @checked = "checked" } : new { @id = ids, @checked="" };` – Rahul Sharma Aug 08 '19 at 09:53
  • @RahulSharma: I tried this but it does not work. I get the error : Error CS0173 Type of conditional expression cannot be determined because there is no implicit conversion between '' and '' – refresh Aug 08 '19 at 09:57
  • Well this is happening because of your conversions are failing while checking for the conditional statements. This answer can help you to understand this syntax: https://stackoverflow.com/questions/18260528/type-of-conditional-expression-cannot-be-determined-because-there-is-no-implicit. For your case, try using the former part of my code comment where `@checked=""` – Rahul Sharma Aug 08 '19 at 10:02
  • Did this work for you? – Rahul Sharma Aug 28 '19 at 09:00

0 Answers0