2013-03-27 94 views
1

我一直在研究Contoso大學教程http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/,特別是第6部分(http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/updating-related-data-with-the-entity-framework-in-an-asp-net-mvc-application),其中課程複選框已添加到教師編輯視圖中,並且教師和課程數據庫表相應地更新。修改更新數據庫記錄以創建新數據庫記錄的方法

不幸的是,本教程不包含如何創建一個新的教師記錄和插入兩個數據庫表。

任何人都可以提供指導如何修改編輯方法嗎?

UPDATE

我是新來的實體框架,但我是正確的思維.Add將同時進行插入或更新?

如果是這樣,我想知道如果我需要改變的是該位

var instructorToUpdate = db.Instructors 
.Include(i => i.OfficeAssignment) 
.Include(i => i.Courses) 
.Where(i => i.InstructorID == id) 
.Single(); 

創建一個新的ID,而不是使用現有的ID? UPDATE OF

END

HTTP POST方法行動

[HttpPost] 
public ActionResult Edit(int id, FormCollection formCollection, string[] selectedCourses) 
{ 
var instructorToUpdate = db.Instructors 
    .Include(i => i.OfficeAssignment) 
    .Include(i => i.Courses) 
    .Where(i => i.InstructorID == id) 
    .Single(); 
if (TryUpdateModel(instructorToUpdate, "", null, new string[] { "Courses" })) 
{ 
    try 
    { 
     if (String.IsNullOrWhiteSpace(instructorToUpdate.OfficeAssignment.Location)) 
     { 
      instructorToUpdate.OfficeAssignment = null; 
     } 

     UpdateInstructorCourses(selectedCourses, instructorToUpdate); 

     db.Entry(instructorToUpdate).State = EntityState.Modified; 
     db.SaveChanges(); 

     return RedirectToAction("Index"); 
    } 
    catch (DataException) 
    { 
     //Log the error (add a variable name after DataException) 
     ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists, see your system administrator."); 
    } 
} 
PopulateAssignedCourseData(instructorToUpdate); 
return View(instructorToUpdate); 
} 

其他方法

private void UpdateInstructorCourses(string[] selectedCourses, Instructor instructorToUpdate) 
{ 
if (selectedCourses == null) 
{ 
    instructorToUpdate.Courses = new List<Course>(); 
    return; 
} 

var selectedCoursesHS = new HashSet<string>(selectedCourses); 
var instructorCourses = new HashSet<int> 
    (instructorToUpdate.Courses.Select(c => c.CourseID)); 
foreach (var course in db.Courses) 
{ 
    if (selectedCoursesHS.Contains(course.CourseID.ToString())) 
    { 
     if (!instructorCourses.Contains(course.CourseID)) 
     { 
      instructorToUpdate.Courses.Add(course); 
     } 
    } 
    else 
    { 
     if (instructorCourses.Contains(course.CourseID)) 
     { 
      instructorToUpdate.Courses.Remove(course); 
     } 
    } 
} 
} 

...

private void PopulateAssignedCourseData(Instructor instructor) 
{ 
var allCourses = db.Courses; 
var instructorCourses = new HashSet<int>(instructor.Courses.Select(c => c.CourseID)); 
var viewModel = new List<AssignedCourseData>(); 
foreach (var course in allCourses) 
{ 
    viewModel.Add(new AssignedCourseData 
    { 
     CourseID = course.CourseID, 
     Title = course.Title, 
     Assigned = instructorCourses.Contains(course.CourseID) 
    }); 
} 
ViewBag.Courses = viewModel; 
} 

編輯視圖

@model ContosoUniversity.Models.Instructor 

@{ 
ViewBag.Title = "Edit"; 
} 

<h2>Edit</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>Instructor</legend> 

    @Html.HiddenFor(model => model.PersonID) 

    <div class="editor-label"> 
     @Html.LabelFor(model => model.LastName) 
    </div> 
    <div class="editor-field"> 
     @Html.EditorFor(model => model.LastName) 
     @Html.ValidationMessageFor(model => model.LastName) 
    </div> 

    <div class="editor-label"> 
     @Html.LabelFor(model => model.FirstMidName) 
    </div> 
    <div class="editor-field"> 
     @Html.EditorFor(model => model.FirstMidName) 
     @Html.ValidationMessageFor(model => model.FirstMidName) 
    </div> 

    <div class="editor-label"> 
     @Html.LabelFor(model => model.HireDate) 
    </div> 
    <div class="editor-field"> 
     @Html.EditorFor(model => model.HireDate) 
     @Html.ValidationMessageFor(model => model.HireDate) 
    </div> 

    <div class="editor-label"> 
     @Html.LabelFor(model => model.OfficeAssignment.Location) 
    </div> 
    <div class="editor-field"> 
     @Html.EditorFor(model => model.OfficeAssignment.Location) 
     @Html.ValidationMessageFor(model => model.OfficeAssignment.Location) 
    </div> 
    <div class="editor-label"> 
     @Html.LabelFor(model => model.OfficeAssignment.Location) 
    </div> 
    <div class="editor-field"> 
     <table style="width: 100%"> 
      <tr> 
       @{ 
        int cnt = 0; 
        List<ContosoUniversity.ViewModels.AssignedCourseData> courses = ViewBag.Courses; 

        foreach (var course in courses) { 
         if (cnt++ % 3 == 0) { 
          @: </tr> <tr> 
         } 
         @: <td> 
          <input type="checkbox" 
            name="selectedCourses" 
            value="@course.CourseID" 
            @(Html.Raw(course.Assigned ? "checked=\"checked\"" : "")) /> 
          @course.CourseID @:&nbsp; @course.Title 
         @:</td> 
        } 
        @: </tr> 
       } 
     </table> 
    </div> 
    <p> 
     <input type="submit" value="Save" /> 
    </p> 
</fieldset> 
} 

<div> 
@Html.ActionLink("Back to List", "Index") 
</div> 

回答

0

問題已經回答here

我想我應該問這個問題之前已經搜索更難!

相關問題