如何保存在ASP.NET MVC形式编辑多个模型? [英] How do I save multiple Models edited in form in ASP.NET MVC?

查看:114
本文介绍了如何保存在ASP.NET MVC形式编辑多个模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将支持对象的批量插入一个视图。我使用实体框架4 Repository模式。

由于将在视图中的多个单独的模型,如何处理结合视图的什么得到的送到仓库模式?

我如何从retreive的形式输入值到控制器?


我已经能够做到的观点,但我无法从控制器检索数据。我用了如下因素语句,但后门柱它h​​eppens是NUL

 公众的ActionResult AddPaymentForRoot_post(PaymentViewModelObject支付){
        返回null;    }


解决方案

我没有用EF,但我对我们的网站类似的要求

。我通过指定为继承查看做到了 System.Web.Mvc.ViewPage< IEnumerable的< MyModelObject>> 然后用一个for循环来构建多个输入段,利用命名需要约定允许模型绑定打造枚举。

我preFER创建具有这是一个枚举属性视图模型(见下面的更新2)。在这个例子中,我们可以说这将是与的IEnumerable&LT的 MyViewModel 对象; MyModelObject> 属性名为 MyModelObjects 。在这种情况下,视图会发挥出如下...

具体来说,确保包括每个输入部分命名为 MyModelObjects.Index 一个隐藏字段,然后让你的财产投入命名为 MyModelObjects [0] .MyProperty ,其中首页隐藏字段的值对应于数组索引的属性名称。

你到底是什么了是这样的,例如:

 < D​​IV>
    <输入类型=隐藏的名字=MyModelObjects.Index值=0/>
    名称:<输入类型=TEXTNAME =MyModelObjects [0] .Name点/>< BR />
    价值:<输入类型=文本名称=MyModelObjects [0] .value的/>
< / DIV>
< D​​IV>
    <输入类型=隐藏的名字=MyModelObjects.Index值=1/>
    名称:<输入类型=TEXTNAME =MyModelObjects [1] .Name点/>< BR />
    价值:<输入类型=文本名称=MyModelObjects [1] .value的/>
< / DIV>
< D​​IV>< A HREF =#ID =的addItem>添加另一个项目< / A>< / DIV>

在某些情况下,我也有JavaScript的(在上面的标记锚点链接触发),将克隆的模板输入部分中,修改的名称来填充索引值,并把它添加到DOM。这样,用户就可以动态地记录添加到视图。

根据您是如何处理你的模型,你也可以通过在有一个属性是IEnumerable的一个视图模型。该视图的打字会改变,但从根本上视图的设计不会的。

更新
使用上面的例子,下面是jQuery的code我用它来添加项目的快速片段。我假设这是一个插入,但要注意的意见,如果你想支持这种类型的接口编辑为好。

首先,我成立了一个模板。虽然这可能是jQuery的完全建成,我只是觉得它更容易做标记和操纵的名称。

 < D​​IV ID =templateDiv的风格=显示:无;>
    <输入类型=隐藏的名字=MyModelObjects.IndexVALUE =0T/>
    名称:<输入类型=TEXTNAME =MyModelObjects [0T] .Name点/>< BR />
    价值:<输入类型=文本名称=MyModelObjects [0T] .value的/>
< / DIV>

您也可以在指定的ID有可能...一个很好的做法,但为了简洁起见,我会离开它的例子。

通过这一点,你可以设置jQuery函数如下:

  VAR CURRENTINDEX = 1 //如果你使用这个编辑,这将被动态地设置为你的项目数
VAR doAddItem =
    功能(){
        CURRENTINDEX ++;
        VAR的newitem = $(#templateDiv)的clone()。
        newItem.attr(ID,项+ CURRENTINDEX); //此处重置ID
        newItem.find(输入[名称= MyModelObjects.Index])VAL(CURRENTINDEX)。
        newItem.find(输入[名称$ =名称])ATTR(名,MyModelObjects [+ CURRENTINDEX +] .Name点)。
        newItem.find(输入[名称$ =值]),ATTR(名,MyModelObjects [+ CURRENTINDEX +] .value的);
        newItem.insertBefore($(本).closest(格)); //含有添加链接的分区前插入它...调整适当布局
        返回false;
    }$(文件)。就绪(函数(){
    $(#的addItem),单击(doAddItem)。
    //在真实世界的应用程序,你很可能希望有一个删除选项,太
});

更新2
有一个有点错误在那里 - 如果你的观点是输入到的IEnumerable<对象> 那么你的字段名称将最终只是被指数 [0] .Name点 / [0] .value的。在我的实际应用程序,我使用具有属性,本身是一个枚举视图模型,所以其实我型我页作为< MyViewModelObject> 。在上面的例子中,该视图模型对象将有一个名为属性 MyModelObject ,这将是一个枚举(更逼真名为 MyModelObjects 复数)。

I need to make a view which will support bulk inserting of objects. I am using the repository pattern with Entity Framework 4.

Since there will be multiple individual models in the View, how do I handle binding the view's model to what get's sent to the Repository?

How do I retreive the input values from the form into the controller?


I have managed to do the view but I am unable to retrieve the data from controller. I used the folowing statement but after the post it heppens to be nul

    public ActionResult AddPaymentForRoot_post(PaymentViewModelObject payments) {


        return null;

    }

解决方案

I haven't used EF yet, but I have a similar requirement on our site. I accomplished it by specifying the View as inheriting System.Web.Mvc.ViewPage<IEnumerable<MyModelObject>> then use a for loop to build multiple input sections, utilizing the naming conventions required to allow the model binder to build the enumerable.

I prefer to create a view model with a property that is an enumerable (see update 2 below). In this example we could say it would be the MyViewModel object with an IEnumerable<MyModelObject> property called MyModelObjects. In this case, the view would play out as follows...

Specifically, make sure to include for each input section a hidden field named MyModelObjects.Index, then have your property inputs named MyModelObjects[0].MyProperty, where the value of the Index hidden field corresponds to the array index in the property names.

What you would end up with would look like this, for example:

<div>
    <input type="hidden" name="MyModelObjects.Index" value="0" />
    Name: <input type="text" name="MyModelObjects[0].Name" /><br />
    Value: <input type="text" name="MyModelObjects[0].Value" />
</div>
<div>
    <input type="hidden" name="MyModelObjects.Index" value="1" />
    Name: <input type="text" name="MyModelObjects[1].Name" /><br />
    Value: <input type="text" name="MyModelObjects[1].Value" />
</div>
<div><a href="#" id="addItem">Add another item</a></div>

In some cases I also have javascript (triggered by the anchor link in the markup above) that will clone a template input section, modify the names to populate the index value, and add it to the DOM. In this way users can dynamically add records to the view.

Depending on how you are handling your models, you may also pass in a ViewModel that has a property that is IEnumerable. The typing for the view would change, but fundamentally the design of the view wouldn't.

UPDATE Using the example above, here's a quick snippet of the jQuery code I use to add items. I'm assuming this is an insert, but note the comments if you wanted to support this type of interface for editing as well.

First, I set up a template. While this could be built totally in jQuery, I just find it easier to do the markup and manipulate the names.

<div id="templateDiv" style="display: none;">
    <input type="hidden" name="MyModelObjects.Index" value="0T" />
    Name: <input type="text" name="MyModelObjects[0T].Name" /><br />
    Value: <input type="text" name="MyModelObjects[0T].Value" />
</div>

You can also specify IDs in there...probably a good practice, but in the interest of brevity I'll leave it out for the example.

With that, you can set up the jQuery function as follows:

var currentIndex = 1 // if you were using this for editing, this would be dynamically set to the number of items you have
var doAddItem =
    function() {
        currentIndex++;
        var newItem = $("#templateDiv").clone();
        newItem.attr("id", "item" + currentIndex); // reset the ID here
        newItem.find("input[name=MyModelObjects.Index]").val(currentIndex);
        newItem.find("input[name$=.Name]").attr("name", "MyModelObjects[" + currentIndex + "].Name");
        newItem.find("input[name$=.Value]").attr("name", "MyModelObjects[" + currentIndex + "].Value");
        newItem.insertBefore($(this).closest("div")); // insert it before the div containing the add link...adjust appropriate to your layout
        return false;
    }

$(document).ready(function() {
    $("#addItem").click(doAddItem);
    // In a real-world app you'd probably want to have a delete option, too
});

UPDATE 2 There's a bit of an error up there - if your view is typed to an IEnumerable<object> then your field names will end up just being Index and [0].Name / [0].Value. In my actual app I use a view model that has a property that is itself an enumerable, so I actually type my pages as <MyViewModelObject>. In the example above, that view model object would have a property called MyModelObject, which would be an enumerable (and more realistically called MyModelObjects plural).

这篇关于如何保存在ASP.NET MVC形式编辑多个模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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