Asp.net mvc 4 如何在一个视图中合并两个实体?

Asp.net mvc 4 如何在一个视图中合并两个实体?,asp.net-mvc-4,views,viewmodel,Asp.net Mvc 4,Views,Viewmodel,我需要显示学生的记录,这是学生的尝试。 应该是这样的 点击查看图片 但这是问题页面,显示了所有正确答案。因为学生尝试页面应该完全相同,只有答案/选项被他们尝试的答案替换,所以当答案正确时,单词将显示为绿色,错误的单词将显示为红色 为了实现这一点,我必须从两个不同的实体中检索两个数据 下面是IQuestion表和QuestionContent是保存模型答案的属性,StudentAttempts表和Answer是保存学生尝试的答案的属性 点击查看图片 如何组合这两个属性以在视图中显示 Student

我需要显示学生的记录,这是学生的尝试。 应该是这样的

点击查看图片

但这是问题页面,显示了所有正确答案。因为学生尝试页面应该完全相同,只有答案/选项被他们尝试的答案替换,所以当答案正确时,单词将显示为绿色,错误的单词将显示为红色

为了实现这一点,我必须从两个不同的实体中检索两个数据

下面是IQuestion表和QuestionContent是保存模型答案的属性,StudentAttempts表和Answer是保存学生尝试的答案的属性

点击查看图片

如何组合这两个属性以在视图中显示

StudentAttemptsController.cs

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using iStellarMobile.Models;

namespace iStellarMobile.Controllers
{
    public class StudentAttemptsController : Controller
    {
        private istellarEntities db = new istellarEntities();

        //
        // GET: /StudentAttempts/

        public ActionResult Index(int id)
        {
            var studentattempts = db.StudentAttempts.Include(s => s.activity).Include(s => s.task).Include(s => s.UserInfo).Where(s => s.StudentID == id);
            return View(studentattempts.ToList());
        }

        //
        // GET: /StudentAttempts/Details/5

        public ActionResult Details(int id = 0)
        {
            StudentAttempt studentattempt = db.StudentAttempts.Find(id);
            if (studentattempt == null)
            {
                return HttpNotFound();
            }
            return View(studentattempt);
        }

        //
        // GET: /StudentAttempts/Create

        public ActionResult Create()
        {
            ViewBag.ActivityID = new SelectList(db.activities, "ActivityID", "ActivityName");
            ViewBag.TaskID = new SelectList(db.tasks, "TaskID", "TaskName");
            ViewBag.StudentID = new SelectList(db.UserInfoes, "ID", "UserName");
            return View();
        }

        //
        // POST: /StudentAttempts/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(StudentAttempt studentattempt)
        {
            if (ModelState.IsValid)
            {
                db.StudentAttempts.Add(studentattempt);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.ActivityID = new SelectList(db.activities, "ActivityID", "ActivityName", studentattempt.ActivityID);
            ViewBag.TaskID = new SelectList(db.tasks, "TaskID", "TaskName", studentattempt.TaskID);
            ViewBag.StudentID = new SelectList(db.UserInfoes, "ID", "UserName", studentattempt.StudentID);
            return View(studentattempt);
        }

        //
        // GET: /StudentAttempts/Edit/5

        public ActionResult Edit(int id = 0)
        {
            StudentAttempt studentattempt = db.StudentAttempts.Find(id);
            if (studentattempt == null)
            {
                return HttpNotFound();
            }
            ViewBag.ActivityID = new SelectList(db.activities, "ActivityID", "ActivityName", studentattempt.ActivityID);
            ViewBag.TaskID = new SelectList(db.tasks, "TaskID", "TaskName", studentattempt.TaskID);
            ViewBag.StudentID = new SelectList(db.UserInfoes, "ID", "UserName", studentattempt.StudentID);
            return View(studentattempt);
        }

        //
        // POST: /StudentAttempts/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(StudentAttempt studentattempt)
        {
            if (ModelState.IsValid)
            {
                db.Entry(studentattempt).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.ActivityID = new SelectList(db.activities, "ActivityID", "ActivityName", studentattempt.ActivityID);
            ViewBag.TaskID = new SelectList(db.tasks, "TaskID", "TaskName", studentattempt.TaskID);
            ViewBag.StudentID = new SelectList(db.UserInfoes, "ID", "UserName", studentattempt.StudentID);
            return View(studentattempt);
        }

        //
        // GET: /StudentAttempts/Delete/5

        public ActionResult Delete(int id = 0)
        {
            StudentAttempt studentattempt = db.StudentAttempts.Find(id);
            if (studentattempt == null)
            {
                return HttpNotFound();
            }
            return View(studentattempt);
        }

        //
        // POST: /StudentAttempts/Delete/5

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            StudentAttempt studentattempt = db.StudentAttempts.Find(id);
            db.StudentAttempts.Remove(studentattempt);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}
IQuestion.cs模型

namespace iStellarMobile.Models
{
    using System;
    using System.Collections.Generic;

    public partial class IQuestion
    {
        public int ID { get; set; }
        public Nullable<int> ActivityID { get; set; }
        public Nullable<int> TaskID { get; set; }
        public Nullable<int> CategoriesID { get; set; }
        public Nullable<bool> Sentence { get; set; }
        public string QuestionContent { get; set; }
        public string ImageURL { get; set; }
        public string CreatedBy { get; set; }
        public Nullable<System.DateTime> CreatedOn { get; set; }
        public string UpdateBy { get; set; }
        public Nullable<System.DateTime> UpdateOn { get; set; }
        public Nullable<int> SchoolID { get; set; }
        public Nullable<int> DLevel { get; set; }
        public Nullable<int> TagID { get; set; }

        public virtual ActivityTask ActivityTask { get; set; }
        public virtual Category Category { get; set; }
        public virtual School School { get; set; }
        public virtual Tag Tag { get; set; }
    }
}
namespace iStellarMobile.Models
{
    using System;
    using System.Collections.Generic;

    public partial class StudentAttempt
    {
        public int ID { get; set; }
        public Nullable<int> ActivityID { get; set; }
        public Nullable<int> TaskID { get; set; }
        public Nullable<int> StudentID { get; set; }
        public string Answer { get; set; }
        public string Score { get; set; }
        public Nullable<int> Attempts { get; set; }
        public string AttemptDate { get; set; }
        public string CorrectAnswer { get; set; }

        public virtual activity activity { get; set; }
        public virtual task task { get; set; }
        public virtual UserInfo UserInfo { get; set; }
    }
}
StudentAttempts.cs模型

namespace iStellarMobile.Models
{
    using System;
    using System.Collections.Generic;

    public partial class IQuestion
    {
        public int ID { get; set; }
        public Nullable<int> ActivityID { get; set; }
        public Nullable<int> TaskID { get; set; }
        public Nullable<int> CategoriesID { get; set; }
        public Nullable<bool> Sentence { get; set; }
        public string QuestionContent { get; set; }
        public string ImageURL { get; set; }
        public string CreatedBy { get; set; }
        public Nullable<System.DateTime> CreatedOn { get; set; }
        public string UpdateBy { get; set; }
        public Nullable<System.DateTime> UpdateOn { get; set; }
        public Nullable<int> SchoolID { get; set; }
        public Nullable<int> DLevel { get; set; }
        public Nullable<int> TagID { get; set; }

        public virtual ActivityTask ActivityTask { get; set; }
        public virtual Category Category { get; set; }
        public virtual School School { get; set; }
        public virtual Tag Tag { get; set; }
    }
}
namespace iStellarMobile.Models
{
    using System;
    using System.Collections.Generic;

    public partial class StudentAttempt
    {
        public int ID { get; set; }
        public Nullable<int> ActivityID { get; set; }
        public Nullable<int> TaskID { get; set; }
        public Nullable<int> StudentID { get; set; }
        public string Answer { get; set; }
        public string Score { get; set; }
        public Nullable<int> Attempts { get; set; }
        public string AttemptDate { get; set; }
        public string CorrectAnswer { get; set; }

        public virtual activity activity { get; set; }
        public virtual task task { get; set; }
        public virtual UserInfo UserInfo { get; set; }
    }
}
Details.cshtml StudentAttempts视图

<fieldset>
    <legend>Classes</legend>

    <div class="editor-label">
            <h2>   @Html.LabelFor(model => model.activity.ActivityName)    </h2>
        </div>
    <div class="display-field">
        @Html.DisplayFor(model => model.activity.ActivityName)
    </div>
    <br />
   <div class="editor-label">
            <h2>   @Html.LabelFor(model => model.task.TaskName)    </h2>
        </div>
    <div class="display-field">
        @Html.DisplayFor(model => model.task.TaskName)
    </div>
    <br />
    <div class="editor-label">
            <h2>   @Html.LabelFor(model => model.UserInfo.UserName)    </h2>
        </div>
    <div class="display-field">
        @Html.DisplayFor(model => model.UserInfo.UserName)
    </div>
    <br />
   <div class="editor-label">
            <h2>   @Html.LabelFor(model => model.Answer)    </h2>
        </div>
    <div class="display-field">
        @Html.DisplayFor(model => model.Answer)
    </div>
    <br />
    <div class="editor-label">
            <h2>   @Html.LabelFor(model => model.Score)    </h2>
        </div>
    <div class="display-field">
        @Html.DisplayFor(model => model.Score)
    </div>
    <br />
   <div class="editor-label">
            <h2>   @Html.LabelFor(model => model.Attempts)    </h2>
        </div>
    <div class="display-field">
        @Html.DisplayFor(model => model.Attempts)
    </div>
    <br />
    <div class="editor-label">
            <h2>   @Html.LabelFor(model => model.AttemptDate)    </h2>
        </div>
    <div class="display-field">
        @Html.DisplayFor(model => model.AttemptDate)
    </div>
    <br />
    <div class="editor-label">
            <h2>   @Html.LabelFor(model => model.CorrectAnswer)    </h2>
        </div>
    <div class="display-field">
        @Html.DisplayFor(model => model.CorrectAnswer)
    </div>
</fieldset>
<br />
<p>   
 @Html.ActionLink("Edit", "/Edit/2",null, new { id=Model.ID, @class="classname" }) 
    <span>  </span>
  @Html.ActionLink("Back", "/Index", null, new { @class="classname" })
</p>

您可以创建一个新的类类型,比如StudentAttempsVM,它为您需要详细信息和尝试的每个类都有一个属性。然后合并您的控制器方法以构建此对象并将其传递给您的视图。

请显示您的代码。可以给我一些示例吗?