c# - MVC razor format to postback viewmodel using groupby -
hi have following viewmodel:
public class vehiclesviewmodel { public ilist<vehicle> vehicles{ get; set; } }
vehicle is:
public class vehicle { public owner owner { get; set; } public make make { get; set; } public string status { get; set; } }
now in razor view:
@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>
so have view can display , alter status vehicle , post controller no problem.
however have new requirement display vehicles on page grouped owner of vehicle. came with:
@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 provide way edit status here</td> </tr> } } </tbody> </table>
the problem have foreach syntax instead of don't know how write , editor status.
can me this?
@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> } }
however, suggest grouping list inside controller instead of view.
Comments
Post a Comment