2016-08-16 58 views
0

我有一個示範課程,組件,子組件和評估:插入值MVC 4

public class Course 
    { 
     public virtual int CourseId { get; set; } 
     (...) 
     public virtual List<Component> Components { get; set; } 
     public virtual List<UserCourse> Users { get; set; } 
    } 

public class Component 
    { 
     public virtual int ComponentId { get; set; } 

     public virtual string Type { get; set; } 
     public virtual string Name { get; set; } 

     public virtual Course Course { get; set; } 

     public virtual List<Subcomponent> Subcomponents { get; set; } 

    } 

public class Subcomponent 
    { 
     public virtual int SubcomponentId { get; set; } 
     public virtual int ComponentId { get; set; } 

     public virtual string TypeSub { get; set; } 
     public virtual string NameSub { get; set; } 
     (...) 

     public virtual int ParentId { get; set; } 
     public virtual Subcomponent Parent { get; set; } 
     public virtual List<Subcomponent> Childs { get; set; } 

     public virtual Component Component { get; set; } 

    } 

public class Evaluation 
    { 
     public virtual int EvaluationId { get; set; } 

     public virtual int ComponentId { get; set; } 
     public virtual Component Component { get; set; } 

     public virtual int UserCourseId { get; set; } 
     public virtual UserCourse User { get; set; } 

     public virtual int Grade { get; set; } 
    } 

我需要給一年級到每個子組件(或組件,如果組件不有子組件)給每個用戶。 首先我有一個觀點,即顯示了用戶和已經給出的成績,像一個指標:

@model SGP.Models.Course 

<table> 
    <tr> 
     <th> 
      Users 
     </th> 

     <th> 
      Grades 
     </th> 
    </tr> 

    @foreach (var x in Model.Users) 
    { 
     <tr> 
      <td> 
       @Html.DisplayFor(modelItem => x.User.UserName) 
      </td> 
      @foreach (var y in x.Evaluation) 
      { 
       <td> 
        <table> 
         <tr> 
          <td> 
           <b>@Html.DisplayFor(modelItem => y.Component.NameComp)</b> 
          </td> 
         </tr> 
         <tr> 
          <td> 
           @Html.DisplayFor(modelItem => y.Grade) 
          </td> 
         </tr> 
        </table> 
       </td> 
      } 
      <td> 
       @Html.ActionLink("Give grade", "Comps", "Evaluation", new { id = x.UserCourseId }, null) 
      </td> 
     </tr> 
    } 
</table> 

然後,當按下「給品位」,我有一個搜索框,選擇組件至極的品位將得到。 搜索將給出該組件的子組件列表。每個組件可以有一個或多個子組件,因此該組件的最終等級是子組件等級的平均值。 子組件的視圖:

@model SGP.Models.Component 

<table> 
    <tr>   
     <th> 
      @Html.DisplayFor(model => model.NameComp) 
     </th>  
     <th> 
      Grade 
     </th> 
    </tr> 

@foreach (var item in Model.Subcomponents) { 
    <tr>  
     <td> 
      @Html.DisplayFor(modelItem => item.NameSub) 
     </td>  

     <td> 
      ??????? 
     </td> 

    </tr> 
} 

</table> 

但這個觀點我需要得到預先選擇的UserCourseId並給了一個檔次,以在選定的子用戶。我怎樣才能做到這一點? 感謝

編輯: 給予等級:

@using (Html.BeginForm("ListaSubs", "Evaluation", FormMethod.Post)) 
    { 
     <table> 
      <tr> 
       <th> 
        @Html.TextBox("text") 
       </th> 
       <td> 
        <input type="submit" id="ButtonSearch" value="Search" /> 
       </td> 
      </tr> 
     </table> 
    } 

和控制器:

[HttpPost, ActionName("ListaSubs")] 
    public ActionResult ListaSubs(string text) 
     { 
     var util = (db.Subcomponents.Where(m => 
     m.ComponentId.Equals(text))); 
     return View("view", util); 
     } 
+0

什麼是「給級」視圖的樣子和它是如何顯示的? – Steve

+0

@Steve現在的問題 – dasdasd

回答

0

我在這種情況下做的,是定義一個包裝類,並用它來查看模型。事情是這樣的:

public class MyWrapperClass 
{ 
    public Course _Course { get; set; } 
    public Component _Component { get; set; } 
    ... 
} 
在你看來

然後,你定義模型:

@model MyWrapperClass 

對於某些控件,你可以簡單地使用以下格式的屬性:

@Html.HiddenFor(model => model._Course.CourseId) 

但在某些情況下,您可能必須爲每個子類創建EditorTemplate或DisplayTemplate,並在視圖中使用這些模板。

要使用Editor模板,您需要在類定義中使用UIHint屬性。

下面是關於如何在剃鬚刀使用的編輯器模板中的一些鏈接:

How to add and use a razor editor template

https://www.simple-talk.com/dotnet/asp-net/extending-editor-templates-for-asp-net-mvc/