在一个剃刀视图中使用2个Mvccheckboxlist和2个不同的实体

时间:2014-04-04 13:47:23

标签: c# entity-framework asp.net-mvc-4 razor checkboxlist

我正在寻找在这种情况下使用MVCCheckboxList库的最佳方法:

2个MVCCheckboxlists,包含2个不同的实体。

示例:

  1. MvccheckboxList a:复选框1,复选框2。 =>使用实体一
  2. MvccheckboxList b:复选框1,复选框2,复选框3 =>使用实体二
  3. 基于此链接 http://www.codeproject.com/Tips/613785/How-to-Use-CheckBoxListFor-With-ASP-NET-MVC

    MvccheckboxList a和MvccheckboxList b工作,不在同一视图中分离。

    我已经有了方向解决方案: 使用包含2个实体的viewmodel并在我的视图中使用这种方式。

    @using MvcCheckBoxList.Model
    @model PortPlus.Data.OpenErp.ViewModels.UnionCategoryViewModel
    @using (Html.BeginForm("UnionCategoryLists", "Client", FormMethod.Post))
    {
        <fieldset>
            <legend></legend>
            <section class="checkBoxListFor">
                <p>
                    @Html.CheckBoxListFor(
                model => model.UnionVM.PostedUnions.OpenErpUnionIds,
                model => model.UnionVM.AvailableUnions,
                union => union.Id,
                union => union.Name,
                model => model.UnionVM.SelectedUnions,
                Position.Horizontal)
                </p>
            </section>
    
        </fieldset>
        <fieldset>
            <legend></legend>
            <section class="checkBoxListFor">
                <p>
                    @Html.CheckBoxListFor(
                model => model.CategoryVM.PostedCategories.OpenErpCategoryIds,
                model => model.CategoryVM.AvailableCategories,
                category => category.Id,
                category => category.Name,
                model => model.CategoryVM.SelectedCategories,
                Position.Horizontal)
                </p>
            </section>
    
        </fieldset>
        <div>
    
            <button class="btn_portplus">@Html.ActionLink("Vorige","Index","Client")</button>
             <input class="btn_portplus" type="submit"
                            value="Volgende" />
        </div>
    }
    

    我的HTTPGET工作正常,但我的HTTPPOST工作正常,更具体的是我希望获得&#34; model =&gt; model.CategoryVM.PostedCategories.OpenErpCategoryIds&#34; /&#34; model.UnionVM.PostedUnions.OpenErpUnionIds&#34;返回值以进行数据库交互,以保存新选定的项目。我需要&#34; OpenErpCategoryIds&#34;和#34; OpenErpUnionIds&#34;,只有那些我需要以某种方式获得的价值,但我不知道它为什么不起作用。

    在我的httpget和httppost(控制器)下面

    [HttpGet]
                public ActionResult UnionCategoryLists(Guid id)
                {
                    var unionscategories = _openerpupdatebusiness.UniversalInitialModel(id);
                    return View(unionscategories);
                }
                [HttpPost]
                public ActionResult UnionCategoryLists(Guid id,UnionCategoryViewModel uc)
                {
                    var newselectedunionscategories = _openerpupdatebusiness.UniversalModel(id, uc);
                    return RedirectToAction("CompanyContacts");
                }
    

    欢迎为我的模块提供任何帮助或更好的解决方案!

1 个答案:

答案 0 :(得分:0)

我用这种方式修复了:(不能给你我的代码,只有我修改它的想法)

2个实体 1包含2个实体的Viewmodel并重新使用链接中的代码

Viewmodel包含双重的PostedEntitieA,PostedEntitieB,AvailableEntitieA,B等等,它在某种程度上是重复的,但在我的情况下,这是解决它的唯一方法,但是如果你有时间让它更干净。