模型ASP.NET MVC绑定数组 [英] ASP.NET MVC bind array in model

查看:138
本文介绍了模型ASP.NET MVC绑定数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是有点两部分的问题(请让我知道,如果他们要分手了)。

This is somewhat a two-part question (please let me know if they should be split up).

1)我有内部包含它的对象的数组一个模型类。我想能够自动绑定这个,所以我可以接受我的控制器,一个 pollModel 参数。

1) I have a model class with an array of objects contained inside it. I would like to be able to bind this automatically so I can accept a single pollModel argument in my controllers.

public class pollResponseModel
{
    public long id { get; set; }
    public long pollID { get; set; }
    public string text { get; set; }
    public long count { get; set; }
}

public class pollModel
{
    public long id;
    public long entID { get; set; }
    public string question { get; set; }
    public DateTime posted { get; set; }
    public DateTime expiration { get; set; }

    public pollResponseModel[] responses { get; set; }
}

问题是,我不知道如何绑定的响应字段,看到它可以是任意大小。好吧,我可以显示在编辑视图时正确绑定,但仅此而已。这使我对我的问题的第二部分:

The problem is that I'm not sure how to bind the responses field, seeing as it can be any arbitrary size. Well, I can bind it properly when displaying the edit view, but that's about it. That leads me to the second part of my question:

2)什么是动态创建和客户端上的一个列表中删除数据的一种可接受的方式,因此,它可以被绑定到一个模型,并在其修饰形式访问的服务器上?我设想创建/移除过程的工作,如iPhone列表界面:一个 + 按钮将增加一个新的元素,和 - 上的每一行数据按钮将从列表中删除。我会想象jQuery是一个恰当的起点,但我的JS技能是非常有限的。

2) What's an acceptable way of dynamically creating and removing data in a list on the client, so that it can be bound to a model and accessed in its modified form on the server? I envision the creation/removal process working like the iPhone list GUI: a single + button will add a new element, and a - button on each row of data will remove it from the list. I would imagine jQuery is an appropriate starting point but my JS skills are very limited.

推荐答案

看看这篇文章菲尔哈克:的模型绑定到一个列表。这正好说明你需要做绑定到列表属性,或者说是复杂对象的属性是什么。

Check out this article by Phil Haack : Model Binding To a List. It explains exactly what you need to do to bind to list properties, or properties that are complex objects.

从本质上讲,你只需要在构建的模型绑定解析它的正确方法的POST数据。本文介绍了如何在表单中添加隐藏的索引字段和再present您的复杂属性。

Essentially you just have to construct your POST data in the correct way for the model binder to parse it. The article explains how to add hidden index fields and represent your complex properties in your form.

这篇关于模型ASP.NET MVC绑定数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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