2013-05-13 75 views
0

嗨,我有以下视图模型:MVC剃刀格式回传视图模型使用GROUPBY

public class VehiclesViewModel 
    { 
     public IList<Vehicle> Vehicles{ get; set; } 
    } 

车辆:

public class Vehicle 
{ 
    public Owner Owner { get; set; } 
    public Make Make { get; set; } 
    public string Status { get; set; } 
} 

现在的Razor视图:

@model VehiclesViewModel 

    <table> 
     <thead> 
     <tr> 
      <th>Owner</th> 
      <th>Make</th> 
      <th>Model</th> 
     </tr> 
     </thead> 
     <tbody> 
    @for(int i=0; i<Model.Vehicles.Count; i++) 
    { 

     <tr> 
      <td>@Vehicle[i].Owner</td> 
      <td>@Vehicle[i].Make</td> 
      <td>@Html.EditorFor(x => x.Vehicles[i].Status)</td> 
     </tr> 

    } 
    </tbody> 
</table> 

所以现在我有一个观点,我可以显示和改变车辆的状态,并将其发回给控制器没有问题。

但是,我现在有一个新的要求,在由车主分组的页面上显示车辆。所以,我想出了:

@model VehiclesViewModel 

<table> 
    <thead> 
    <tr> 
     <th>Make</th> 
     <th>Model</th> 
    </tr> 
    </thead> 
    <tbody> 
@foreach(var vehicleGroup in @Model.Vehicles.GroupBy(x => x.Owner.Name)) 
{ 
    <tr> 
    <td colspan="2">@vehicleGroup.First().Owner.Name</td> 
    </tr> 
    foreach(var vehicle in vehicleGroup) 
    { 

     <tr> 
     <td>@vehicle.Make.Name</td> 
     <td>Need to provide a way to edit the status here</td> 
     </tr> 

    } 
} 
</tbody> 
</table> 

的问题是,现在我有这样的foreach的语法,而不是因为我不知道怎么写和编辑状态。

任何人都可以帮助我吗?

回答

0

也许只是用一个简单的输入标签:

<input type="text" name="[email protected]"value="@vehicle.Status" /> 
1
@foreach(var vehicleGroup in Model.Vehicles.GroupBy(x => x.Owner.Name)) 
{ 
    <tr> 
     <td colspan="2">@vehicleGroup.First().Owner.Name</td> 
    </tr> 

    foreach(var vehicle in vehicleGroup) 
    { 
     <tr> 
       <td> @vehicle.Make.Name </td> 
       <td> @Html.EditorFor(m => vehicle.Status) </td> 
     </tr>  
    } 
} 

不过,我建议你的分组,而不是控制器的视图中的列表。