【问题标题】:How to combine two entities in one view?如何在一个视图中组合两个实体?
【发布时间】:2014-07-10 02:23:36
【问题描述】:

我需要显示学生的记录,即学生尝试。 它应该看起来像这样。

https://www.dropbox.com/s/pmazbug2j8xwehe/Example.PNG(点击查看图片)

但这是显示所有正确答案的问题页面。对于学生尝试页面应该完全一样,只有答案/选项被他们尝试的答案替换,所以当它是正确的时候,这个词会是绿色的,错误的会是红色的。

为此,我必须从两个不同的实体中检索 2 个数据。

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

https://www.dropbox.com/s/f92f8zvk9qn1n8p/DB%20Tables.PNG(点击查看图片)

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

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

StudentAttempts.cs(模型)

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(学生尝试视图)

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

【问题讨论】:

  • 请出示您的代码..

标签: asp.net-mvc-4 views viewmodel


【解决方案1】:

您可以创建一个新的类类型(例如“StudentAttempsVM”),它为您需要的每个类(详细信息和尝试)提供一个属性。然后整合你的 Controller 方法来构建这个对象并将它传递给你的视图。

【讨论】:

  • 能给我一些例子吗?
猜你喜欢
  • 2015-10-19
  • 2019-11-14
  • 1970-01-01
  • 1970-01-01
  • 2021-04-10
  • 2013-09-20
  • 2015-11-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多