MVC不确定如何检索模型中的相关对象

时间:2016-11-06 08:35:53

标签: asp.net-mvc

我将viewmodel传递给看起来像这样的视图

assessment
    questions   
        [0]
            QuestionText
            QuestionID
        [1]
            QuestionText
            QuestionID

    Answers
        [0]
            TheAnswer
            QuestionID

基本上我的观点有一个问题列表和可能已经选择的任何答案列表。

我需要显示所有问题和任何选择的答案 问答的链接是QuestionID

这是我在视图中的剃刀代码,它循环所有问题

@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 what to put here-->)
        </div>
    </li>
}

如何检索问题的所选答案?

修改

以下是我的模特

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; }
    }

2 个答案:

答案 0 :(得分:0)

试试这个 <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:id="@+id/t1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:padding="5dip" android:text="text1" android:textStyle="normal|bold" /> <TextView android:id="@+id/t2" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:padding="5dip" android:text="text2" android:textStyle="normal|bold" /> <TextView android:id="@+id/t3" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:padding="5dip" android:text="text3" android:textStyle="normal|bold" /> <Button android:id="@+id/button6" android:layout_width="50dp" android:layout_height="50dp" android:background="@android:drawable/ic_input_add" /> </LinearLayout> ,如果它不为空,则显示相应的字段

答案 1 :(得分:0)

试试这个:

private List<TabLayout.Tab> tabList;

private void initTabLayout() {
    tabList = new ArrayList<>();
    tabList.add(mTabLayout.newTab().setIcon(R.drawable.ic_fire_white));
    tabList.add(mTabLayout.newTab().setIcon(R.drawable.ic_apps_white));
    tabList.add(mTabLayout.newTab().setIcon(R.drawable.ic_account_plus_white));
    tabList.add(mTabLayout.newTab().setIcon(R.drawable.ic_help_white));
    for (TabLayout.Tab tab : tabList){
        mTabLayout.addTab(tab);
    }
    mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            tabList.get(position).select();
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });

}