模型绑定和继承ASP MVC 5

时间:2016-08-13 16:44:27

标签: c# asp.net json asp.net-mvc inheritance

基类:AbstractBuildBlock,派生:TextBlock,ImageBlock,EmptyBlock,....

阻止此处:网站 - > Pages [someIndex] - >行[someIndex] - > BuildBlocks

字段BuildBlocks的类型为AbstractBuildBlock,因此当我在BuildBlocks中将Site保存到DB时,每条记录都有一个描述符AbstractBuildBlock。我尝试在BuildBlockRepository中执行下一步:

    switch(obj.ContentType)
            {
                case "text":
                    obj = obj as TextBlock;
                    context.BuildBlocks.Add(obj);
                    break;
            }

obj = obj as TextBlock objnull。原因是obj的类型为AbstractBuildBlock。我在msdn发现这段代码应该可以工作:

BaseClass a = new DerivedClass()
DerivedClass b = a as DerivedClass

所以我需要在模型绑定中重现这段代码。这是ajax请求:

$('.saveSite').click(function () {
    $.ajax({
        url: '/Site/Update',
        data: { site: getSite() },
        method: 'POST',
        success: function (data) {
            console.log('Success save');
        },
        error: function (data) {
            debugBox(data);
        }
    });
});

和此请求的mvc操作

    public string Update(Site site)
    {
        siteRepository.Add(site);
        return "Success";
    }

所以我以json形式发送Site,BuildBlocks也以json的形式存在于此站点中,但当然它们的(块)类型不是AbstractBuildBlock,它们都是TextBlock,ImageBlock等,并且具有值的字段。

问题:网站有字段BuildBlocks,其类型是AbstractBuildBlock,模型绑定器执行以下操作:

buildBlock = new AbstractBuildBlock(); //loose derived classes fields and posibility to convert it in DerivedClass
buildBlocks.push(buildBlock)

但我需要这样的东西

switch(buildBlock.contenType) {
    case "text" : buildBlock = new TextBlock();buidlBlocks.push(buildBlock);
}

1 个答案:

答案 0 :(得分:0)

JSON NET Custom deserializer not work at all

ASP MVC 5 How to read object from request with the help of JSON NET

查看上面两个链接中的答案,描述了正确的ajax调用

以下服务器代码

mvc行动

public string Update(Site site)
    {
        TextBlock block = site.Pages[0].Rows[0].BuildBlocks[0] as TextBlock;
        siteRepository.Add(site);
        return "Success";
    }

AbstractJsonCreationConverter我在Infrastructure文件夹

中创建了它
public abstract class AbstractJsonCreationConverter<T> : JsonConverter
{
    protected abstract T Create(Type objectType, JObject jsonObject);

    public override bool CanConvert(Type objectType)
    {
        return typeof(T).IsAssignableFrom(objectType);
    }

    public override object ReadJson(JsonReader reader, Type objectType,
      object existingValue, JsonSerializer serializer)
    {
        var jsonObject = JObject.Load(reader);
        var target = Create(objectType, jsonObject);
        serializer.Populate(jsonObject.CreateReader(), target);
        return target;
    }

    public override void WriteJson(JsonWriter writer, object value,
   JsonSerializer serializer)
    {
        throw new NotImplementedException();
    }
}

并在同一个文件夹中具体类

public class JsonBuildBlockConverter : AbstractJsonCreationConverter<AbstractBuildBlock>
{
    protected override AbstractBuildBlock Create(Type objectType, JObject jsonObject)
    {
        var type = jsonObject["contentType"].ToString();
        switch(type)
        {
            case "text":
                return new TextBlock();
            default:
                return null;
        }
    }
}

还有一个基础设施课程

internal class SiteModelBinder : System.Web.Mvc.IModelBinder
{
    public object BindModel(ControllerContext controllerContext, System.Web.Mvc.ModelBindingContext bindingContext)
    {
        // use Json.NET to deserialize the incoming Position
        controllerContext.HttpContext.Request.InputStream.Position = 0; // see: https://stackoverflow.com/a/3468653/331281
        Stream stream = controllerContext.RequestContext.HttpContext.Request.InputStream;
        var readStream = new StreamReader(stream, Encoding.UTF8);
        string json = readStream.ReadToEnd();
        return JsonConvert.DeserializeObject<Site>(json, new JsonBuildBlockConverter());
    }
}

最后一个类是ModelBinder,它将被调用来解析Site类型的变量,使其工作需要在ApplicationStart()中的Global.asax.cs中注册它

 protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
        ModelBinders.Binders.Add(typeof(Site), new SiteModelBinder()); //RegisterModelBinder for Site
    }

这就是全部。