asp.net mvc - MVC unsure how to retrieve related objects in a model -
i pass viewmodel view looks this
assessment questions [0] questiontext questionid [1] questiontext questionid answers [0] theanswer questionid
basically view has list of questions , list of answers may have chosen.
i need display questions , chosen answer link between question , answer questionid
this razor code in view loops questions
@foreach (var question in model.assessment.questions) { <li class="row"> <div class="span9"> @html.raw(question.questiontext) </div> <div class="span3"> @html.raw(<--no idea put here-->) </div> </li> }
how can retrieve selected answer question?
edit
here models
public class assessment { public int id { get; set; } public status status { get; set; } public datetime assessmentdate { get; set; } public int areaid { get; set; } public virtual area area { get; set; } public virtual icollection<question> questions { get; set; } public virtual icollection<answer> answers { get; set; } } public class question { public int id { get; set; } public string questiontext { get; set; } public int displaynumber { get; set; } public int displayorder { get; set; } public string questionhelp { get; set; } public bool isactive { get; set; } public int categoryid { get; set; } public int processid { get; set; } public virtual category category { get; set; } public virtual process process { get; set; } public virtual icollection<assessment> assessment { get; set; } public virtual icollection<answer> answers { get; set; } } public class answer { public int id { get; set; } public int value { get; set; } public int questionid { get; set; } public virtual question question { get; set; } public virtual icollection<assessment> assessments { get; set; } }
try model.assessment.chosenanswers.firstordefault(a => a.questionid == question.questionid)
, if it's not null show respective field
Comments
Post a Comment