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

根據question.IsSelected值,應該選擇或不選擇複選框。剃刀單選按鈕MVC

但是無論真正的IsSelected屬性是否總是檢查Radiobutton。你能指出哪裏錯誤檢查屬性請

回答

4

如果你給它任何checked屬性它將被設置爲檢查。我可以根據IsSelected屬性添加整個checked='checked'值,當值爲false時省略它。

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

謝謝,工程就像魅力。 –

1

你可以做這樣的

@{ 
    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/>