我是相當新的MVC3框架,並因與編輯一些問題,或者我的一個表創建數據模型。我在此解決方案中使用了EF代碼第一種方法。MVC3模型視圖問題與有2個外鍵同桌
我有了2個字段鏈接到同一個表一個表。例如:[entry]表可以引用2個不同的學生。下面是該代碼:
public class Entry
{
public int EntryID { get; set; }
public int DanceEventID { get; set; }
public int StudentID { get; set; }
public int? SecondaryStudentID { get; set; }
public int InstructorID { get; set; }
public int DanceID { get; set; }
public int DanceLevelID { get; set; }
public int StyleID { get; set; }
public int? EntryNumber { get; set; }
public virtual DanceEvent DanceEvent { get; set; }
public virtual Student Student { get; set; }
public virtual Student SecondaryStudent { get; set; }
public virtual Instructor Instructor { get; set; }
public virtual Dance Dance { get; set; }
public virtual DanceLevel DanceLevel { get; set; }
public virtual Style Style { get; set; }
}
public class Student
{
public int StudentID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public int DanceLevelID { get; set; }
public virtual DanceLevel DanceLevel { get; set; }
public int StudioID { get; set; }
public virtual Studio Studio { get; set; }
public int GenderID { get; set; }
public virtual Gender Gender { get; set; }
public string FullName
{
get
{
return FirstName + " " + LastName;
}
}
}
,我有是,當我去編輯或從視圖保存和我進入一個SecondaryStudent,這不是節約的問題。看來這個視圖只生成一個Student對象。
這是一個MVC3的或限制很可能我只是做錯事了。
編輯: 這裏是創建視圖和控制器代碼。
@model HeatBuilder.Models.Entry
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>Entry</legend>
<div class="editor-label">
@Html.LabelFor(model => model.DanceEventID, "DanceEvent")
</div>
<div class="editor-field">
@Html.DropDownList("DanceEventID", String.Empty)
@Html.ValidationMessageFor(model => model.DanceEventID)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.StudentID, "Student")
</div>
<div class="editor-field">
@Html.DropDownList("StudentID", String.Empty)
@Html.ValidationMessageFor(model => model.StudentID)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.StudentID, "Second Student")
</div>
<div class="editor-field">
@Html.DropDownList("StudentID", String.Empty)
@Html.ValidationMessageFor(model => model.SecondaryStudentID)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.InstructorID, "Instructor")
</div>
<div class="editor-field">
@Html.DropDownList("InstructorID", String.Empty)
@Html.ValidationMessageFor(model => model.InstructorID)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.DanceID, "Dance")
</div>
<div class="editor-field">
@Html.DropDownList("DanceID", String.Empty)
@Html.ValidationMessageFor(model => model.DanceID)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.DanceLevelID, "DanceLevel")
</div>
<div class="editor-field">
@Html.DropDownList("DanceLevelID", String.Empty)
@Html.ValidationMessageFor(model => model.DanceLevelID)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.StyleID, "Style")
</div>
<div class="editor-field">
@Html.DropDownList("StyleID", String.Empty)
@Html.ValidationMessageFor(model => model.StyleID)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HeatBuilder.Models;
namespace HeatBuilder.Controllers
{
public class EntryController : Controller
{
private HeatBuilderContext db = new HeatBuilderContext();
//
// GET: /Entry/
public ViewResult Index()
{
var entries = db.Entries.Include(e => e.DanceEvent).Include(e => e.Student).Include(e => e.SecondaryStudent).Include(e => e.Instructor).Include(e => e.Dance).Include(e => e.DanceLevel).Include(e => e.Style);
return View(entries.ToList());
}
//
// GET: /Entry/Details/5
public ViewResult Details(int id)
{
Entry entry = db.Entries.Find(id);
return View(entry);
}
//
// GET: /Entry/Create
public ActionResult Create()
{
ViewBag.DanceEventId = new SelectList(db.DanceEvents, "DanceEventId", "EventName");
ViewBag.StudentID = new SelectList(db.Students, "StudentID", "FullName");
ViewBag.SecondaryStudentID = new SelectList(db.Students, "SecondaryStudentID", "FullName");
ViewBag.InstructorId = new SelectList(db.Instructors, "InstructorID", "FullName");
ViewBag.DanceID = new SelectList(db.Dances, "DanceId", "DanceName");
ViewBag.DanceLevelId = new SelectList(db.DanceLevels, "DanceLevelID", "Description");
ViewBag.StyleID = new SelectList(db.Styles, "StyleID", "StyleDescription");
return View();
}
//
// POST: /Entry/Create
[HttpPost]
public ActionResult Create(Entry entry)
{
if (ModelState.IsValid)
{
db.Entries.Add(entry);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.DanceEventID = new SelectList(db.DanceEvents, "DanceEventId", "EventName", entry.DanceEventID);
ViewBag.StudentID = new SelectList(db.Students, "StudentID", "FullName", entry.StudentID);
ViewBag.SecondaryStudentID = new SelectList(db.Students, "SecondaryStudentID", "FullName", entry.SecondaryStudentID);
ViewBag.InstructorID = new SelectList(db.Instructors, "InstructorID", "FullName", entry.InstructorID);
ViewBag.DanceID = new SelectList(db.Dances, "DanceId", "DanceName", entry.DanceID);
ViewBag.DanceLevelID = new SelectList(db.DanceLevels, "DanceLevelID", "Description", entry.DanceLevelID);
ViewBag.StyleID = new SelectList(db.Styles, "StyleID", "StyleDescription", entry.StyleID);
return View(entry);
}
//
// GET: /Entry/Edit/5
public ActionResult Edit(int id)
{
Entry entry = db.Entries.Find(id);
ViewBag.DanceEventID = new SelectList(db.DanceEvents, "DanceEventId", "EventName", entry.DanceEventID);
ViewBag.StudentID = new SelectList(db.Students, "StudentID", "FullName", entry.StudentID);
ViewBag.SecondaryStudentID = new SelectList(db.Students, "SecondaryStudentID", "FullName", entry.SecondaryStudentID);
ViewBag.InstructorID = new SelectList(db.Instructors, "InstructorID", "FullName", entry.InstructorID);
ViewBag.DanceID = new SelectList(db.Dances, "DanceId", "DanceName", entry.DanceID);
ViewBag.DanceLevelID = new SelectList(db.DanceLevels, "DanceLevelID", "Description", entry.DanceLevelID);
ViewBag.StyleID = new SelectList(db.Styles, "StyleID", "StyleDescription", entry.StyleID);
return View(entry);
}
//
// POST: /Entry/Edit/5
[HttpPost]
public ActionResult Edit(Entry entry)
{
if (ModelState.IsValid)
{
db.Entry(entry).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.DanceEventID = new SelectList(db.DanceEvents, "DanceEventId", "EventName", entry.DanceEventID);
ViewBag.StudentID = new SelectList(db.Students, "StudentID", "FullName", entry.StudentID);
ViewBag.SecondaryStudentID = new SelectList(db.Students, "SecondaryStudentID", "FullName", entry.SecondaryStudentID);
ViewBag.InstructorID = new SelectList(db.Instructors, "InstructorID", "FullName", entry.InstructorID);
ViewBag.DanceID = new SelectList(db.Dances, "DanceId", "DanceName", entry.DanceID);
ViewBag.DanceLevelID = new SelectList(db.DanceLevels, "DanceLevelID", "Description", entry.DanceLevelID);
ViewBag.StyleID = new SelectList(db.Styles, "StyleID", "StyleDescription", entry.StyleID);
return View(entry);
}
//
// GET: /Entry/Delete/5
public ActionResult Delete(int id)
{
Entry entry = db.Entries.Find(id);
return View(entry);
}
//
// POST: /Entry/Delete/5
[HttpPost, ActionName("Delete")]
public ActionResult DeleteConfirmed(int id)
{
Entry entry = db.Entries.Find(id);
db.Entries.Remove(entry);
db.SaveChanges();
return RedirectToAction("Index");
}
protected override void Dispose(bool disposing)
{
db.Dispose();
base.Dispose(disposing);
}
}
}
謝謝。
你混淆了一些技術。 MVC與你的持久層無關。請向我們展示您在保存條目時使用的代碼。 – driis 2012-01-07 16:15:40
謝謝。我繼續編輯原始帖子,將控制器代碼和創建視圖/ – edgel1k 2012-01-07 16:35:21