2012-10-19 16 views
1

我有一個新手問題,我試圖瞭解過去幾天。希望有人能夠幫助我理解編程流程。查看模型和視圖中的複選框

假設我有一個模型,將信息存儲在數據庫中:

public class Student 
{ 
    public int studentID { get; set; } 
    public string studentName { get; set; } 
    public strin studentGrade {get; set; } 
} 

public class StudentDBContext : DbContext 
{ 
    public DbSet<Student> Students { get; set; } 
} 

,我想它顯示到視圖,額外的複選框,這樣我就可以選擇哪個學生被提升到下一個年級。我讀了一個辦法做到這一點是通過投入視圖模型:

public class StudentViewModel 
{ 
    public bool promoted { get; set; } 
    public Student stu { get; set; } 
} 

但我堅持這個是做它的方式嗎?如果是的話,你如何看待它將顯示旁邊有一個複選框的所有學生。之後,我想更新複選框勾選的學生的所有成績。例如:

學生A,學生B,學生D從一年級升入二年級。所以我想展示學生,勾選學生A,B和D並提交以更新成績。

一步一步的例子將不勝感激。

更新1:

控制器:

[HttpGet] 
    public ViewResult CheckBox() 
    { 
     var studentViewModels = db.Students.Select(m => new StudentViewModel() 
     { 
      stu = m 
     }).ToList(); 

     return View(studentViewModels); 
    } 

    [HttpPost] 
    public ActionResult CheckBox(IList<studentViewModel> list) 
    { 

     foreach (var stuUpdate in list.Where(m => m.promoted)) 
     { 
      var stuRow = db.Students.Find(stuUpdate.stu.studentID); 
      stuRow.studentName = stuRow.studentName + "1"; 
      db.Entry(stuRow).State = EntityState.Modified; 
      db.SaveChanges(); 
      return RedirectToAction("CheckBox"); 
     } 
     return RedirectToAction("CheckBox"); 
    } 

查看:

@model IList<School.ViewModels.StudentViewModel> 

@using (Html.BeginForm()) 
{ 
<table> 
<tr> 
    <th> 

    </th> 
    <th> 
     student ID 
    </th> 
    <th> 
     student name 
    </th> 
    <th> 
     student grade 
    </th> 
</tr> 
@foreach (var item in Model) { 
    <tr> 
     <td> 
      @Html.CheckBoxFor(modelItem => item.promoted) 
      @Html.HiddenFor(modelItem => item.stu.studentID) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.stu.studentID) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.stu.studentName) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.stu.studentGrade) 
     </td> 
    </tr> 
} 

</table> 
<input type="submit" value="save" /> 
} 

但是目前由以下錯誤命中: 值不能爲空。 參數名:源

Source Error: 
foreach (var stuUpdate in list.Where(m => m.promoted)) 
+0

幫助....任何人? – blurryMVC

回答

2

一個非常基本的「一步一步」(在這麼做了,所以我可能做了一些錯誤,但你的想法)。

你總是有幾種方法來做這些事情,所以......只是真的把它當成一個樣本,並找到其他的例子來獲得其他的想法。首先,在你的控制器中,你將有一個GET操作(查看列表)。

[HttpGet] 
public ViewResult StudentList() { 
    //retrieve all students. With the Select, we create a new instance of StudentViewModel for each student. 
    //assuming StudentDbContext being a property of your controller, if it's not, you can instantiate a new one (in a using clause) 
    var studentViewModels = StudentDbContext.Students 
       .Select(m => new StudentViewModel() { 
        stu = m 
        //we don't say nothing about promoted : 
        //it will be there as "false" by default, which is probably what we want. 
       }).ToList(); 
    //now we've got a list of StudentViewModel. This will be the model of our view 
    return View(studentViewModels); 

} 

然後,我們已經有了一個視圖,StudentList.cshtml

在該視圖中,我們將顯示一個表,其中爲每個學生一個線:studentId(隱藏在這種情況下),這個名字(僅顯示),等級(僅顯示)和複選框。

我們需要一個for循環(不是foreach)來獲得精確的模型綁定。

@model IList<StudentViewModel> 

@using (Html.BeginForm()) { 
<table> 
    <tr> 
    <th>Student name</th> 
    <th>Student grade</th> 
    <th>Promote</th> 
    </tr> 
    @for (var i = 0; i < Model.Count(); i++) { 
    <tr> 
     <td>@Html.HiddenFor(m => Model[i].Student.studentID) 
      @Html.DisplayFor(m => Model[i].Student.studentName) 
     </td> 
     <td>@Html.DisplayFor(m => Model[i]Student.studentGrade)</td> 
     <td>@Html.CheckBoxFor(m => Model[i].promoted)</td> 
    </tr> 
    } 
</table> 
<input type="submit" value="save" /> 
} 

這種形式會導致另一個POST操作(相同的名稱得到一個,這取決於你在你的Html.BeginForm什麼)

[HttpPost] 
public ActionResult StudentList(IList<StudentViewModel> list) { 
    //we treat only the lines where checkbox has been checked 
    foreach (var studentViewModel in list.Where(m => m.promoted) { 
     var student = StudentDBContext.GetById(studentViewModel.Student.studentID);//get student entity instance from context 
     student.studentGrade ="<new value>"; 
     StudentDBContext.SaveChanges();//save changes, you must have such a method somewhere. 
    } 
    return Action("StudentList"); 
} 

小細節:

儘量尊重一些真正基本的「慣常」做法:例如在c#中,屬性應該以大寫字母開頭(所以StudentGrade,StudentName,Promoted等)。

+0

感謝您的指導。但是,我收到錯誤說在POST方法中值不能爲空。有什麼我想念的嗎?另外感謝您提供有關這些屬性的指針。我實際上正在自學MVC,有沒有任何指導或教程可以幫助我學習它? – blurryMVC

+0

@blurryMVC如果您的實際代碼是您在問題中編輯的內容,請按照我的回答中的指示,將您的視圖中的「foreach」循環更改爲「for」循環 –

+0

謝謝您提供的建議,但只需稍作調整在post方法上。最初它只會更新第一行,只有在我移出「return Action(」StudentList「);」 for循環之外它更新兩個。我也可以知道foreach如何在循環工作時不工作?我讀論壇,我認爲foreach是取代循環本身。是否存在不應使用foreach的特殊情況? – blurryMVC