2016-10-22 7 views
0

私はこのモデルがあります:のC#、MVC、代わりに、テーブルのHTML

public class CoursesTeacher 
{ 
    public int Id { get; set; } 
    public string FullName { get; set; } 
    public IEnumerable<string> CourseName { get; set; } 
    public IEnumerable<int> CourseId { get; set; } 
} 

私はかみそりで代わりにテーブルのhtmlを作成したいと思います。 先生には複数のコースがあります。

その

that

ように私はこのコードを書いた:

<table class="table table-bordered"> 
<tr> 
    <th> 
     @Html.ActionLink("Teacher Name", "RequestsTeachers") 
    </th> 
    <th> 
     @Html.ActionLink("Corese Name", "RequestsTeachers") 
    </th> 
    <th> 
     Edit 
    </th> 
</tr> 

@foreach (var item in Model) 
     { 

    <tr> 
     <td rowspan="@item.CourseName.Count()"> 
      @Html.DisplayFor(modelItem => item.FullName) 
     </td> 

     @foreach (var courseName in item.CourseName) 
      { 
      <td> 
       @Html.DisplayFor(modelItem => courseName) 
      </td> 
     } 

     @foreach (var courseId in item.CourseId) 
      { 

      <td> 
       <div class="pull-right"> 
        @Html.NoEncodeActionLink("<span class='glyphicon glyphicon-pencil'></span>", "Edit", "EditFinancial", "Control", routeValues: new { courseId = courseId }, htmlAttributes: new { data_modal = "", @class = "btn btn-default" }) 
       </div> 
      </td> 
     } 

    </tr> 
} 

をしかし正しくない作成: correct

正しい作成する方法?

+0

CoursesTeacherのCourseIdとCourseNameは右同数の要素を持っているのですか? –

答えて

0

に型付けされたビューでは、私はコースの情報を表すためのviewmodelを移動して、作成したい:

public class CourseInfoModel 
{ 
    public string CourseName { get; set; } 

    public int CourseId { get; set; } 

    public bool IsFirstCourse { get; set; } 
} 

その後モデルクラスでは、より適切な方法でデータを取得するreadonlyプロパティを作成します。

ビューで今

は、テーブルにあなたが望む方法をレンダリングするのは簡単です:

@model IEnumerable<MvcTable.Models.CoursesTeacher> 
<table class="table table-bordered"> 
    <tr> 
     <th> 
      @Html.ActionLink("Teacher Name", "RequestsTeachers") 
     </th> 
     <th> 
      @Html.ActionLink("Course Name", "RequestsTeachers") 
     </th> 
     <th> 
      Edit 
     </th> 
    </tr> 

    @foreach (var item in Model) 
    { 
     foreach (var courseItem in item.Courses) 
     { 
      <tr> 
       @if (courseItem.IsFirstCourse) 
       { 
        <td rowspan="@item.Courses.Count"> 
         @Html.DisplayFor(modelItem => item.FullName) 
        </td> 
       } 

       <td> 
        @Html.DisplayFor(modelItem => courseItem.CourseName) 
       </td> 
       <td> 
        <div class="pull-right"> 
         <a href="@Url.Action("EditFinancial", "Control", new {courseItem.CourseId})" data-modal="" class="btn btn-default"><span class='glyphicon glyphicon-pencil'></span></a> 
        </div> 
       </td> 
      </tr> 
     } 
    } 
</table> 
0

代わりに、CourseNameとCourseIdsを別々のリストに保持する。コースを表現するクラスを作成して使用してみませんか?

public class CourseVm 
{ 
    public int Id { set; get;} 
    public string Name { set; get;} 
} 
public class CoursesTeacher 
{ 
    public int Id { set;get;} 
    public string FullName { set;get;} 
    public List<CourseVm> Courses { set;get;} 
} 

と強くコレクションCoursesTeacher

@model IEnumerable<CoursesTeacher>` 
<table> 
@foreach(var t in Model) 
{ 
    <tr> 
     <td>@t.FullName</td> 
     <td> 
     <table> 
      @foreach(var c in t.Courses) 
      { 
       <tr><td>@c.Name</td> 
        <td><button>Some button</button></td> 
       </tr> 
      } 
     </table> 
     </td> 
    </tr> 
} 
</table> 
関連する問題