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