gpt4 book ai didi

asp.net - 在 MVC View 中对集合进行分组并在 Controller 中更新模型?

转载 作者:行者123 更新时间:2023-12-04 23:51:59 25 4
gpt4 key购买 nike

我有一个显示来自 ViewModel 的集合的 View ,用户可以在其中更新每个集合项的属性(“熟练程度”)。

这是 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>

还有 EditorFor 模板:

@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>

这是我的操作方法(我知道,使用索引 View 似乎有点奇怪,我可能会更改它,但暂时不要介意):

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

现在,尽管这工作正常,但我意识到我需要能够通过 Program.Category 属性对 ProgramSkill 项目进行分组。但是我会遇到几个问题:

如何将它们分组并在 ViewModel 中将它们传递给 View ?

对象是 Program、ProgramSkill 和 Consultant,其中 Program 和 Consultant 之间存在多对多关系,ProgramSkill 是联结表(需要它,因为它具有附加属性“Level”)。

如果我有一个 Linq 表达式来根据这个对它们进行分组,并且可以将它变成一个 View 可以使用的 ViewModel,我将如何在回发到 Controller 之后再次更新模型?

似乎代码会变得更复杂,如果可以的话,只是为了按 Program.Category 对 ProgramSkill 项目进行分类。但同时,如果item没有分类的话,View的可用性会很差...

有人知道解决这个问题的好方法吗?

最佳答案

我认为你想要做的是添加另一个 View 模型

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

然后在你的 Controller 中 Index()

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

希望这行得通。这只是伪代码

关于asp.net - 在 MVC View 中对集合进行分组并在 Controller 中更新模型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5433710/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com