我需要显示学生的记录,即Student Attempts。 看起来应该是这样的。
https://www.dropbox.com/s/pmazbug2j8xwehe/Example.PNG(点击查看图片)
但是这是显示所有正确答案的问题页面。对于Student Attempts页面应该完全相同,只有答案/选项被他们尝试的答案所取代,所以当它正确时,单词将为绿色,错误的单词将为红色。
为此,我必须从两个不同的实体中检索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(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>
答案 0 :(得分:0)
您可以创建一个新的类类型(类似于&#34; StudentAttempsVM&#34;),它具有您需要的每个类的属性(详细信息和尝试)。然后合并您的Controller方法以构建此对象并将其传递给您的视图。