Hari Gillala
Hari Gillala

Reputation: 11916

Razor radio button MVC

<input id="@question.QuestionId" type="radio" value="@question.QuestionDescription" name="@string.Format("name_{0}", question.Group)" [email protected]"checked":false /> @question.QuestionDescription

Depending on the question.IsSelected value the checkbox should be selected or not selected.

But regardless of true or false of the IsSelected property Radiobutton is always checked. Can you point where the error in checked attribute please

Upvotes: 1

Views: 3514

Answers (2)

archil
archil

Reputation: 39491

You could do it like this

@{
    string checkedAttribute = string.Empty;
    if (question.IsSelected)
    {
        checkedAttribute = "checked=\"checked\"";
    }
}
<input id="@question.QuestionId" type="radio" value="@question.QuestionDescription" name="@string.Format("name_{0}", question.Group)" @checkedAttribute/>

Upvotes: 1

tvanfosson
tvanfosson

Reputation: 532445

If you give it anything for the checked attribute it will be set to checked. I would optionally add the entire checked='checked' value based on the IsSelected property, omitting it when the value is false.

<input id="@question.QuestionId" type="radio" value="@question.QuestionDescription" name="@string.Format("name_{0}", question.Group)" @(question.IsSelected?"checked='checked'":"") /> @question.QuestionDescription

Upvotes: 4

Related Questions