Group a collection in MVC View and update a model in a controller?

I have a view that displays a collection from ViewModel, where the user can update the property for each item in the collection ("Level" of language proficiency).

Here View:

@model Consultants.ViewModels.ProgramsViewModel @{ ViewBag.Title = "Programkunskaper"; } <h2>@ViewBag.Title</h2> <div id="formDiv"> @using (Html.BeginForm(null, null, FormMethod.Post, new { id = "myForm" })) { @Html.ValidationSummary(true) <fieldset> <legend>Ny arbetserfarenhet</legend> <table> <tr> <th> Program </th> <th> Nivå </th> </tr> @Html.EditorFor(m => m.ProgramSkills, "ProgramSkills") @*Using editorformodel instead of for loop according to tip here: http://stackoverflow.com/questions/5420471/use-dropdownlistfor-with-foreach-in-asp-net-mvc-view*@ </table> <p> <input type="submit" value="Spara" /> </p> </fieldset> } </div> 

And the EditorFor template:

 @model Consultants.Models.ProgramSkill <tr> <td>@Model.Program.Name </td> <td> @Html.DropDownListFor( model => model.Level, new SelectList(new[] { 0, 1, 2, 3, 4, 5 }, Model.Level ) ) </td> </tr> 

Here are my action methods (I know, using the Index view seems a little strange, and I will probably change that, but don't pay attention to it):

  public ActionResult Index() { Consultant consultant = _repository.GetConsultantByUserName(User.Identity.Name); ViewBag.Consultant = consultant; if (consultant.ProgramSkills.Count == 0) { List<Program> programs = _repository.GetAllPrograms(); foreach (var program in programs) { consultant.ProgramSkills.Add(new ProgramSkill { Program = program }); } _repository.Save(); } ProgramsViewModel vm = new ProgramsViewModel(); vm.ProgramSkills = consultant.ProgramSkills.ToList(); return View(vm); } [HttpPost] public ActionResult Index(ProgramsViewModel vm, FormCollection collection) { Consultant consultant = _repository.GetConsultantByUserName(User.Identity.Name); List<ProgramSkill> programSkills = consultant.ProgramSkills.ToList(); for (int i = 0; i < programSkills.Count; i++) { var programSkill = programSkills[i]; programSkill.Level = vm.ProgramSkills[i].Level; } _repository.Save(); vm.ProgramSkills = programSkills; return View(vm); } 

Now, although this works well, I realized that I need to be able to group ProgramSkill objects using the Program.Category property. But then I will get a couple of problems:

How do I group them and pass them in the ViewModel to the view?

The objects are Program, ProgramSkill and Consultant, where there is a Many-to-Many relationship between the Program and the Consultant, and ProgramSkill is a conversion table (necessary because it has the additional Level property).

And if I had a Linq expression to group them according to this, and I could turn it into a ViewModel that can use View, how would I update the model again after sending it back to the controller?

It seems that the code will be much more complicated, if at all possible, only to categorize ProgramSkill objects by Program.Category. But at the same time, the performance will be very poor usability if the elements are not classified ...

Does anyone know a good solution for this?

+3
source share
1 answer

I think you want to add another view model

 ProgramSkillsModel { string Category {get;set;} IList<ProgramSkill> Skills {get;set;} } 

Then in your controller Index ()

 ProgramsViewModel vm = new ProgramsViewModel(); vm.ProgramSkills = consultant.ProgramSkills.GroupBy(c => c.Category) .Select(c => new ProgramSkillsModel{ Category = c.Key, Skills = c.ToList(), }); 

Hope this works. This is just pseudo code.

0
source

Source: https://habr.com/ru/post/1345098/


All Articles