CslaModelBinder不具约束力的孩子名单(CslaModelBinder not bind

2019-10-21 18:53发布

我有以下业务类

public class EditableRoot:Csla.BusinessBase<EditableRoot>
{
    public string Name { get; private set; }
    public int Id { get; private set; }

    public static EditableRoot New() {
        return DataPortal.Create<EditableRoot>();
    }

    public static readonly PropertyInfo<EditableChildList> ChildListProperty = RegisterProperty<EditableChildList>(c => c.ChildList, RelationshipTypes.Child);
    public EditableChildList ChildList
    {
        get { return GetProperty(ChildListProperty); }
        private set { SetProperty(ChildListProperty, value); }
    }
    protected override void DataPortal_Create()
    {
        ChildList = EditableChildList.New();
    }

}
public class EditableChildList : Csla.BusinessListBase<EditableChildList,EditableChild>
{
    public static EditableChildList New() { return DataPortal.CreateChild<EditableChildList>(); }
}
public class EditableChild : Csla.BusinessBase<EditableChild>
{
    public static readonly PropertyInfo<string> AssignedByProperty = RegisterProperty<string>(c => c.AssignedBy);
    public string AssignedBy
    {
        get { return GetProperty(AssignedByProperty); }
        private set { LoadProperty(AssignedByProperty, value); }
    }
    public static readonly PropertyInfo<int> DocTypeIDProperty = RegisterProperty<int>(c => c.DocTypeID);
    public int DocTypeID
    {
        get { return GetProperty(DocTypeIDProperty); }
        set { SetProperty(DocTypeIDProperty, value); }
    }
    public static EditableChild New(int docTypeId) { return DataPortal.CreateChild<EditableChild>(docTypeId); }
    void Child_Create(int docTypeId)
    {
        DocTypeID = docTypeId;
        AssignedBy = "AssignedBy" + docTypeId;
    }

}

我有控制器

 public class ComplexTypeController : Csla.Web.Mvc.Controller, Csla.Web.Mvc.IModelCreator
{
    //
    // GET: /ComplexType/
    public ActionResult Create()
    {
        EditableRoot type = EditableRoot.New();
        ViewData.Model = type;
        return View();
    }

    [HttpPost]
    public ActionResult Create(EditableRoot complexType, FormCollection collection, string submit)
    {
        if (submit != "Create")
        {
            Random rand = new Random();

            complexType.ChildList.Add(EditableChild.New(rand.Next()));

        }
        ViewData.Model = complexType;
        return View();
    }

    public object CreateModel(Type modelType)
    {
        if (modelType == typeof(EditableRoot))
            return EditableRoot.New();
        else if (modelType == typeof(EditableChildList))
            return EditableChildList.New();
        else if (modelType == typeof(EditableChild))
            return EditableChild.New(0);
        else
            return Activator.CreateInstance(modelType);
    }
}

我有看法

 @model EditableRoot

 @{
     ViewBag.Title = "Create";
 }

 <h2>Create</h2>
 @using (Html.BeginForm())
 {
     @Html.DisplayFor(m => m.Name);
     @Html.HiddenFor(m => m.Id);
     <table>
         <thead>
             <tr>
                 <th>Child Type Name</th>
                 <td>Child Type Id</td>
             </tr>
         </thead>


    <tbody>
            @for (int i = 0; i < Model.ChildList.Count(); i++)
            {
                <tr>
                    <td>
                        @Html.TextBoxFor(a => Model.ChildListIdea.AssignedBy)
                    </td>
                    <td>
                        @Html.TextBoxFor(a => Model.ChildListIdea.DocTypeID)
                    </td>
                </tr>
            }
        </tbody>
    </table>
   <input name="submit" type="submit" id="submit" value="Create" />
    <input name="submit" type="submit" id="process" value="Add Child" />                       
}

当我点击“添加子”按钮添加一个EditableChild然后单击“创建”按钮EditableRoot对象的childList物业公共ActionResult的创建(EditableRoot复杂类型,收集的FormCollection,串提交)调用未绑定。

换句话说子列表EditableRoot.ChildList没有约束,没有项目在列表中,即使在查看HTML遵循结合复杂类型的列表中的约定。 当我在浏览器中查看实际的HTML中EditableRoot.ChildList项目发出的行是否存在且正确命名。

但是,我从GitHub上的CslaModelBinder,并把它放到我的项目和有线了MVC默认模型绑定器来使用它。 然后,我改变了CslaModelBinder方法

公共覆盖对象BindModel(ControllerContext controllerContext,ModelBindingContext的BindingContext)

看起来像这样

public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
            {
                //if (typeof(Csla.Core.IEditableCollection).IsAssignableFrom((bindingContext.ModelType)))
                //    return BindCslaCollection(controllerContext, bindingContext);

                var suppress = bindingContext.Model as Csla.Core.ICheckRules;
                if (suppress != null)
                    suppress.SuppressRuleChecking();
                var result = base.BindModel(controllerContext, bindingContext);
                return result;
            }

一切工作。 该EditableRoot.ChildList属性绑定,预期项目在列表中。

最终我的CslaModelBinder方法的改进

公共覆盖对象BindModel(ControllerContext controllerContext,ModelBindingContext的BindingContext)

评论出结合CSLA IEditableCollection的处理,因此该方法最终调用基类,DefaultModelBinder的BindModel方法。 其工作原理。

但是,如果我用我的修改CslaModelBinder将我遇到的问题在其他地方?

如果DefaultModelBinder可以处理那么为什么Csla.Core.IEditableCollection类型的结合

如果(typeof运算(Csla.Core.IEditableCollection).IsAssignableFrom((bindingContext.ModelType)))返回BindCslaCollection(controllerContext,的BindingContext);

???

Answer 1:

大约有针对孩子的集合结合,以开放的票来解决问题已知问题。

见https://github.com/MarimerLLC/csla/issues/19



文章来源: CslaModelBinder not binding child list