2017-12-20 16 views
1

私は特定のバッチに登録されている生徒の出席パネルを作成しています。そのために、そのバッチに割り当てられたクラスの数に応じて、チェックボックスの数とともに、生徒の記録を表示します。すべてが正しく表示されますが、1行のみのチェックボックスは値をポストに持ち、他の行の残りのチェックボックスはポストされません。各行の生徒の詳細がリストに訂正されて掲載されています。以下は チェックボックスリスト項目はmvcに掲載されていません5

が私のコードである

StudentAttendance.cs

public class StudentAttendance 
    { 
     public List<Models.User> userlist { get; set; } 
     public List<Models.Days> days { get; set; } 
    } 

InstructorController.cs

public ActionResult AssignedStudents(string id) 
     { 
      Models.StudentAttendance viewmodel = new Models.StudentAttendance(); 
      //viewmodel.studentbatch = new Models.StudentBatch(); 
      //viewmodel.user = new Models.User(); 

      Context.Instructor instructor = new Context.Instructor(); 

      viewmodel.userlist = new List<Models.User>(); 
      viewmodel.days = new List<Models.Days>(); 
      viewmodel.userlist = instructor.lstAssignedStudents(id); 

      Context.Batches contBatch = new Context.Batches(); 
      var days = contBatch.SelectDays(id); 
      int totaldays = contBatch.CalculateDays(days); 

      var duration = contBatch.GetallBatchList().Where(p => p.Id == id); 
      var batchduration = (from c in duration where c.Id == id select c.Duration).ToList(); 
      string d = batchduration[0].ToString(); 

      int totalduration = contBatch.GetBatchDuration(d); 

      int TotalCheckBoxes = totalduration * totaldays; 

      List<string> getdays = contBatch.getdaysinList(days, totalduration); 
      List<Models.Days> day = new List<Models.Days>(); 

      for (int i = 0; i < TotalCheckBoxes; i++) 
      { 
       day.Add(new Models.Days { dayid = i, dayname = getdays[i], ischecked = false }); 

      } 

      viewmodel.days = day; 

      return View(viewmodel); 
     } 

[HttpPost] 
     public ActionResult MarkAttendance(Models.StudentAttendance viewmodel) 
     { 
      Models.StudentAttendance viewmodel1 = new Models.StudentAttendance(); 
      //viewmodel.studentbatch = new Models.StudentBatch(); 
      //viewmodel.user = new Models.User(); 
      return View(); 
     } 

AssignedStudents.cshtml

@model WebApplication1.Models.StudentAttendance 

@{ 
    ViewBag.Title = "AssignedStudents"; 
} 

<h2>AssignedStudents</h2> 

<p> 
    @Html.ActionLink("Create New", "Create") 
</p> 

@using (Html.BeginForm("MarkAttendance","Instructor", FormMethod.Post)) 
{ 
<table class="table"> 
    <tr> 

     <th>@Html.DisplayName("First Name")</th> 
     <th>@Html.DisplayName("Last Name")</th> 
    </tr> 

@for (int j = 0; j < Model.userlist.Count; j++) 
{ 

    <tr> 
     <td>@Html.HiddenFor(m=>Model.userlist[j].Id)</td> 
     <td>@Html.EditorFor(m => Model.userlist[j].FirstName)</td> 
     <td>@Html.EditorFor(m => Model.userlist[j].LastName)</td> 
     @for (int i = 0; i < Model.days.Count; i++) 
     { 
      <td> 
       @Html.CheckBoxFor(m => Model.days[i].ischecked) 
       @Model.days[i].dayname 
       @Html.HiddenFor(m => Model.days[i].dayid) 
       @Html.HiddenFor(m => m.days[i].dayname) 
      </td> 
     } 
    </tr> 
} 
</table> 

<div class="form-group"> 
    <div class="col-md-offset-2 col-md-10"> 
     <input type="submit" id="Attendance" value="Create" class="btn btn-default" /> 
    </div> 
</div> 


} 
+0

重複した 'name'属性(重複する' id'属性は無効なhtmlです)を持つ重複したチェックボックスを作成するだけです。 'DefaultModelBinder'は最初に一致する名前だけをバインドします。あなたが達成したいことは明確ではありません。それぞれの 'User'は' Days'のコレクションを持っていますか? (その場合あなたのモデルは間違っています) –

+0

はい、各ユーザーには日のコレクションがあります。 – user100020

+0

次に、User(Student?)のビューモデルが必要です。これには、Daysのコレクションプロパティが含まれていて、ネストされた 'for'ループを使用します。 –

答えて

0

viewmodelをコントローラクラスのListとして使用して問題を解決しました。

StudentAttendance.cs

List<DayVM> days = new List<DayVM> 
{ 
    new DayVM(), 
    new DayVM() 
}; 

List<StudentVM> model = new List<StudentVM> 
{ 
    new StudentVM{ Id = 1, FirstName = "Bob", Days = days }, 
    new StudentVM{ Id = 2, FirstName = "john", Days = days }, 
} 
return View(model); 

とビュー

@for(int i = 0; i < Model.Count; i++) 
{ 
    ... elements for properties of Student 
    @for(int j = 0; j < Model[i].Days.Count; j++) 
    { 
     @Html.CheckBoxFor(m => m[i].Days[j].IsSelected) 
     .... 
    } 
} 

内側とポストアクションで作られたステファン・ミュエッケへ

public ActionResult MarkAttendance(List<Models.StudentVM> lst) 
{ 
..... 
} 

すべてのおかげで値を収集するためのリストを取りますかんたんです。