asp.net mvc使用多态数组属性绑定传入对象

qf9go6mv  于 2021-09-29  发布在  Java
关注(0)|答案(0)|浏览(217)

我以前没有使用过定制活页夹,所以如果我让这种方式变得比应该的更难,我会提前道歉。
我有一个抽象类字段

public class Form
{
    public Int32 Id { get; set; }
    public String Name { get; set; }
    public Int32 Version { get; set; }
    public Int32 PreviousVersion { get; set; }
    public Models.Field[] FormFields { get; set; }
}
``` `FormFields` 是一个抽象类

public abstract class Field : iField
{
public Int32 Id { get; set; }
public Int32 FormId { get; set; }
public Int32 Order { get; set; }
public String Name { get; set; }
public Int32 FieldType { get; set; }
public Boolean RequireValue { get; set; }
}

为了简单起见,我将仅列出几个从字段派生的类作为示例

public sealed class Textbox : Field
public sealed class Dropdown: Field

在绑定期间,这将创建一个数组(formfields),其中包含从字段派生的不同对象类型的混合。我在网上找到了几个例子,其中一些建议使用bindproperty/setproperty,而另一些建议在createmodel期间构建对象。我考虑过使用valueprovider手动构建表单对象,但它都是在 `System.Web.Mvc.DictionaryValueProvider<object>` 这将是乏味的解析所有这些。当我访问bindproperty时,它似乎已经决定它是一个字段数组,我似乎无法迭代每个数组项来更改绑定。
现在有没有办法重新定义数组?
似乎有一种更简单的方法,我可以说如果field.fieldtype=1,那么绑定为textbox。
有人能给我一些指导,告诉我如何完成这项任务吗?
我还应该提到调用方是来自angularjs的$http帖子是否相关。
我的mvc接收器,接受$http angularjs调用

public JsonResult UpdateForm([ModelBinder(typeof(CustomModelBinder))]Models.Form Form)

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题