2016-10-22 3 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

CourseId и CourseName из CoursesTeacher действительно имеют одинаковое число элементов правых? –

ответ

0

Я бы и создать ViewModel для представления информации для курса:

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

    public int CourseId { get; set; } 

    public bool IsFirstCourse { get; set; } 
} 

Тогда в классе модели создайте свойство readonly, чтобы получить данные более подходящим образом:

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; } 

    public IList<CourseInfoModel> Courses 
    { 
     get 
     { 
      if (CourseName == null || CourseId == null || CourseId.Count() != CourseName.Count()) 
       throw new Exception("Invalid data"); //courses and ids must have the same number of elements 

      var list = new List<CourseInfoModel>(); 
      for (int i = 0; i < CourseName.Count(); i++) 
      { 
       list.Add(new CourseInfoModel 
       { 
        IsFirstCourse = i == 0, 
        CourseId = CourseId.ElementAt(i), 
        CourseName = CourseName.ElementAt(i), 
       }); 
      } 

      return list; 
     } 
    } 
} 

Теперь в представлении легко вынести стол так, как вы хотите:

@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> 
Смежные вопросы