使用带接口的MVC4自定义DefaultModelBinder不会填充其他属性

时间:2014-06-09 02:36:06

标签: c# asp.net-mvc-4 model-binding defaultmodelbinder

好的..所以我有一个界面,当我保存看起来像这样的时候我会传回控制器(删除大多数只是为了让想法得到解决)。如果需要,我可以添加更多细节,但我宁愿不要压倒所有人。

public interface IProjectElement
{        
    string Name { get; set; }
    int ElementTypeID { get; set; }
    ...
}
public class BaseProjectElement : IProjectElement
{

    public int ElementTypeID { get; set; }
    public string AdditionalInformation { get; set; }
    ... 
}    

然后我有几个接口的实现,但这里的例如......

public class SQLElement : BaseProjectElement
{
    public int? DatabasePlatformID { get; set; }
}

从Global.asax中开始 - 开始

ModelBinders.Binders.Add(typeof(IProjectElement), new ProjectElementModelBinder());

所以这就是问题所在。下面的代码确定对象的类型并创建适当的对象。我认为发生的是我可以看到所有值都在bindingContent.ValueProvider数据中发送,但有趣的是,属性集合仅列出了接口的值。 base.CreateModel将创建我需要的类型,但只使用接口中的数据填充对象。我可以用一些丑陋的代码来解决它,以设置我设置DatabasePlatformID的其他属性,但是我的一些类有几个额外的属性。我可能在这里遗漏了一些明显的东西。这不是非常可维护的设置这样的附加属性。

public class ProjectElementModelBinder : DefaultModelBinder 
{
    protected override object CreateModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Type modelType)
    {
        string modelName = bindingContext.ModelName;
        var elementType = (int)bindingContext.ValueProvider.GetValue(string.Format("{0}.ElementType", modelName)).ConvertTo(typeof(int));
        Type instantiationType = null;
        if (elementType == 1)
        {
            instantiationType = typeof(IISElement);
        }
        else if (elementType == 2)
        {
            instantiationType = typeof(SharePointElement);
        }
        else if (elementType == 3)
        {
            instantiationType = typeof(SQLElement);
        }
        else if (elementType == 12)
        {
            instantiationType = typeof(SharedElement);
        }
        else
        {
            instantiationType = typeof(BaseProjectElement);
        }
        var obj = base.CreateModel(controllerContext, bindingContext, instantiationType);
        if (elementType == 3)
        {
            PropertyInfo pi = obj.GetType().GetProperty("DatabasePlatformID");
            int databasePlatformID = (int)bindingContext.ValueProvider.GetValue(string.Format("{0}.DatabasePlatformID", modelName)).ConvertTo(typeof(int));
            pi.SetValue(obj, databasePlatformID, null);
        }
        return obj;
    }
}

请注意,我也尝试使用我在其他地方看到的代码来实现它,但是我看到的更糟糕的是对象根本没有填充。

        var obj=Activator.CreateInstance(instantiationType);
        bindingContext.ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(null, instantiationType);
        bindingContext.ModelMetadata.Model = obj;
        return obj;

这是我的控制器

public ActionResult SaveElements(Guid projectID, List<IProjectElement> selectedElements, List<ProjectElementRelationshipsDAO> selectedRelationships)
    {
        using (ManagedHostingHelper helper = new ManagedHostingHelper())
        {
            BasePageViewModel viewModel = helper.SaveElements(projectID, selectedElements, selectedRelationships);
            return Json(new
            {
                ViewModel = viewModel,
                JsonRequestBehavior.AllowGet
            });
        } 
    }

最后,这是调用控制器的javascript。使用knockout.js ..

self.SaveElements = function () {
    var saveURL = new UrlHelper().SaveElements();
        data: ko.toJSON({ projectID: self.ProjectID(), selectedElements: self.SelectedElements(), selectedRelationships: self.SelectedRelationships() }),
        type: "post", contentType: "application/json",
        success: function (result) {
            if (result.ReturnStatus == true) {
                self.GoForward();
            }
            else { alert('error'); }
        }
    });
};

SelectedElements是knockout.js observableArray。下面是可以传递的javascript对象。请注意,此数组将包含实现IProjectElement的元素集合。我还没有显示所有不同类型,但它们都有一组基本数据并添加其他属性。

// base class - see class diagram in folder as types should match this fairly closely
function BaseProjectElement(data) {
    var self = this;
    self.ProjectElementID = ko.observable(data.ProjectElementID);
    self.ElementName = ko.observable(data.ElementName);
    ... more properties
    self.BaseElementValidationGroup = ko.validatedObservable({
        Name: self.Name
    });
    self.IsBaseElementValid = ko.computed(function () {
        return self.BaseElementValidationGroup().errors().length == 0;
    });
};
function SQLElement(data) {
    var self = this;
    ko.utils.extend(self, new BaseProjectElement(data));
    self.DatabasePlatformID = ko.observable(data.DatabasePlatformID).extend({ required: { params: true, message: ' ' } });
    self.SQLElementValidationGroup = ko.validatedObservable({
        DatabasePlatformID: self.DatabasePlatformID
    });
    self.IsSQLElementValid = ko.computed(function () {
        return (self.SQLElementValidationGroup().errors().length == 0) && self.IsBaseElementValid();
    });
};

1 个答案:

答案 0 :(得分:0)

好的..我以为我曾经做过类似的事情,但这里有什么对我有用.. ModelMetaData的设置与我在几个地方见过的线路如

bindingContext.ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(null, instantiationType);

VS

public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        string modelName = bindingContext.ModelName;
        var elementType = (int)bindingContext.ValueProvider.GetValue(string.Format("{0}.ElementType", modelName)).ConvertTo(typeof(int));
        if (elementType == 1)
        {
            bindingContext.ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(() => new IISElement(), typeof(IISElement));
        }
        else if (elementType == 2)
        {
            bindingContext.ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(() => new SharePointElement(), typeof(SharePointElement));
        }
        else if (elementType == 3)
        {
            bindingContext.ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(() => new SQLElement(), typeof(SQLElement));
        }
        else if (elementType == 12)
        {
            bindingContext.ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(() => new SharedElement(), typeof(SharedElement));
        }
        else
        {
            bindingContext.ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(() => new BaseProjectElement(), typeof(BaseProjectElement));
        }
        return base.BindModel(controllerContext, bindingContext);
    }