集团在MVC视图收集和更新模型控制器? [英] Group a collection in MVC View and update model in controller?

查看:107
本文介绍了集团在MVC视图收集和更新模型控制器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从一个视图模型,用户可以更新为每个集合项(熟练的级别)。一个属性显示集合的视图

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

这里的景观:

@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模板:

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 weird and I'll probably change that, but never mind that for now):

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

现在,即使这工作得很好,我意识到,我需要能够以小组ProgramSkill项目由它的Program.Category财产。但后来我会得到几个问题:

Now, even though this works fine, I realized that I need to be able to group the ProgramSkill items by it's Program.Category property. But then I'll get a couple of problems:

我如何组并把它传给视图模型他们查看?

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

的对象是项目,ProgramSkill和顾问,在这里有一个很多方案和顾问之间有许多关系,ProgramSkill是结台(必要的,因为它有附加属性的级别)。

The objects are Program, ProgramSkill and Consultant, where there's a Many to Many relationship between Program and Consultant, and ProgramSkill is the junction table (needed because it has the additional property "Level").

如果我是让他们根据这个LINQ的前pression到组,可以使之成为一个视图模型,该视图可以消耗,我将如何再次回发至控制器后更新模型?

And if I were to have a Linq expression to group them according to this, and could make it into a ViewModel that the View can consume, how would I update the model again after posting back to the Controller?

看来code会得到很多更复杂,如果能够在全部完成,只是由Program.Category分类的ProgramSkill项目。但与此同时,该视图将具有实用性非常差,如果项目未分类...

It seems the code will get a lot more complicated, if it can be done at all, just for categorizing the ProgramSkill items by Program.Category. But at the same time, the View will have very poor usability if the items are not categorized...

任何人都知道这个一个好的解决方案?

Anyone know a good solution for this?

推荐答案

我想你想要做的就是添加其他视图模型是什么

I think what you want to do is 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(),
});

希望工程。这只是伪code

Hopefully that works. It's just pseudo code

这篇关于集团在MVC视图收集和更新模型控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆