当我添加一个带有Entity Framework的控制器时,它会生成视图,但它会错过像Teacher和Student这样的字段,它只会创建ProjectName和Discription字段和视图。我想要的是从学生和教师表中下载数据。 Microsoft Virtual Acadmey的MusicStore教程编写了相同的代码,然后出现了下拉,但我遇到了问题。
(我是MVC模式的新手) 编译代码并运行它
Project.cs(模态类)
{
public class Project
{
public int ID { get; set; }
public int StudentID { get; set; }
public int TeachersID { get; set; }
public string ProjectName { get; set; }
public Teachers Teachers { get; set; }
public Student Student1 { get; set; }
public Student Student2 { get; set; }
public Student Student3 { get; set; }
public string Discription { get; set; }
}
}
控制器
using DataCollection.Models;
namespace DataCollection.Controllers
{
public class ProjectsController : Controller
{
private DataCollectionContext db = new DataCollectionContext();
// GET: Projects
public ActionResult Index()
{
return View(db.Projects.ToList());
}
// GET: Projects/Details/5
public ActionResult Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Project project = db.Projects.Find(id);
if (project == null)
{
return HttpNotFound();
}
return View(project);
}
// GET: Projects/Create
public ActionResult Create()
{
return View();
}
// POST: Projects/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "ID,ProjectName,Discription")] Project project)
{
if (ModelState.IsValid)
{
db.Projects.Add(project);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(project);
}
// GET: Projects/Edit/5
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Project project = db.Projects.Find(id);
if (project == null)
{
return HttpNotFound();
}
return View(project);
}
// POST: Projects/Edit/5
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "ID,ProjectName,Discription")] Project project)
{
if (ModelState.IsValid)
{
db.Entry(project).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(project);
}
// GET: Projects/Delete/5
public ActionResult Delete(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Project project = db.Projects.Find(id);
if (project == null)
{
return HttpNotFound();
}
return View(project);
}
// POST: Projects/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(int id)
{
Project project = db.Projects.Find(id);
db.Projects.Remove(project);
db.SaveChanges();
return RedirectToAction("Index");
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
}
}
我的观点如下
@model IEnumerable<DataCollection.Models.Project>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.ProjectName)
</th>
<th>
@Html.DisplayNameFor(model => model.Discription)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.ProjectName)
</td>
<td>
@Html.DisplayFor(modelItem => item.Discription)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.ID }) |
@Html.ActionLink("Details", "Details", new { id=item.ID }) |
@Html.ActionLink("Delete", "Delete", new { id=item.ID })
</td>
</tr>
}
</table>
教师模式
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
namespace DataCollection.Models
{
public class Teachers
{
public int TeachersID { get; set; }
[Required]
[StringLength(100,ErrorMessage ="Enter First Name")]
[Display(Name = "First Name")]
public string FName { get; set; }
[Required]
[StringLength(100, ErrorMessage = "Enter Last Name")]
[Display(Name = "Last Name")]
public string LName { get; set; }
//[Display(Name = "First Name")]
[Required]
[StringLength(100, ErrorMessage = "Enter Designation")]
public string Designation { get; set; }
[Display(Name = "Project Evulation Committee")]
public Boolean PREC { get; set; }
[Display(Name = "Evaluation Committee")]
public Boolean EC { get; set; }
[Display(Name = "Project Coordinator")]
public Boolean ProjCoordinator { get; set; }
public virtual ICollection<Project> Project { get; set; }
}
}
(欢迎评论,谢谢)