I have defined a model as below-
public class Question
{
public int Id { get; set; }
public string Title { get; set; }
public int UserSelection { get; set; }
public Question()
{
this.Options = new List<Option>();
}
public virtual ICollection<Option> Options { get; set; }
}
Since ICollection
does not have indexer, I am facing problem in binding ICollection Options
to the view.
@model List<LakshyaMvc.Models.Question>
<ol class="Quest">
@for (int i = 0; i < Model.Count; i++)
{
var quest = Model[i];
<li>
@Html.LabelFor(x => quest.Title, quest.Title)
<ol class="Opt">
@for (int j = 0; j < quest.Options.Count; j++)
{
@*Indexer not availabl here *@
<li class="Opt">
@Html.RadioButtonFor(o => quest.Options[j].Title, quest.Options[j].Title, new { @name = "uniqueRadio" })
</li>
}
</ol>
</li>
}
</ol>
The temporary solution I am going to adapt is create partial view for displaying Options. defining model as below -
@model List<LakshyaMvc.Models.Option>
Is this the correct & general approach to bind ICollection to the view ?