3

我是相當新的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); 
    } 
} 
} 

謝謝。

+1

你混淆了一些技術。 MVC與你的持久層無關。請向我們展示您在保存條目時使用的代碼。 – driis 2012-01-07 16:15:40

+0

謝謝。我繼續編輯原始帖子,將控制器代碼和創建視圖/ – edgel1k 2012-01-07 16:35:21

回答

1

在您的看法中,您使用了兩次StudentID,您應該在第二個下拉列表中使用SecondaryStudentID。此外,您完全使用DropDownLists錯誤。 StudentID是從學生名單中選擇的當前選擇的StudentID。您不要將SelectList指定爲StudentID。您需要創建一個名爲StudentList或類似的學生的單獨列表。

ViewBag.ListOfStudents = new SelectList(db.Students, "StudentID", "FullName"); 

您應該使用DropDownListFor。

<div class="editor-label"> 
    @Html.LabelFor(model => model.StudentID, "Student") 
</div> 
<div class="editor-field"> 
    @Html.DropDownListFor(model => model.StudentID, ListOfStudents, "Student") 
    @Html.ValidationMessageFor(model => model.StudentID) 
</div> 


<div class="editor-label"> 
    @Html.LabelFor(model => model.SecondaryStudentID, "Second Student") 
</div> 
<div class="editor-field"> 
    @Html.DropDownListFor(model => model.SecondaryStudentID, ListOfStudents, String.Empty) 
    @Html.ValidationMessageFor(model => model.SecondaryStudentID) 
</div> 
1

如果使用代碼第一次(這樣的外鍵關係不是從數據庫結構而...假設它存在),和你有一個實體的兩個不同的領域,這兩者都是外鍵第二個實體,您可以定義哪個外鍵用於每個子對象。你可能只需要這個做了兩個學生對象,但下面的例子是爲你的外鍵關係中的每一個完成的:

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; } 
    [ForeignKey("DanceEventID")] 
    public virtual DanceEvent DanceEvent { get; set; } 
    [ForeignKey("StudentID")] 
    public virtual Student Student { get; set; } 
    [ForeignKey("SecondaryStudentID")] 
    public virtual Student SecondaryStudent { get; set; } 
    [ForeignKey("InstructorID")] 
    public virtual Instructor Instructor { get; set; } 
    [ForeignKey("DanceID")] 
    public virtual Dance Dance { get; set; } 
    [ForeignKey("DanceLevelID")] 
    public virtual DanceLevel DanceLevel { get; set; } 
    [ForeignKey("StyleID")] 
    public virtual Style Style { get; set; } 
} 

雖然這是否與不節能的數據無法確定導致您的問題當然。

0

我也是這個實體模型的新手。但我認爲你所指的是一個稱爲自我引用的東西。例如,員工可能有一個名爲經理的字段。現在,由於經理也是一名員工,所以該領域指向該表中的員工表。這是如何實施它的一個非常好的問題。也許用它的主鍵和一個外鍵創建另一個表格,這個外鍵可以在這種情況下從管理員字段填充到沒有其他數據的學生表中。如果允許,這將建立1對1關係。讓我知道你是如何解決這個問題的。

或者你可以用學生證的學生secondarly ID與請求 ,當它再次呈現請求學生證