小编Vla*_*dka的帖子

模型与Asp.Core中的继承绑定

我需要接受来自用户的对象列表:

public async Task<IActionResult> CreateArticle(List<InformationBlockModel> informationBlocks)
    {
        ...
    }
Run Code Online (Sandbox Code Playgroud)

ModelBinder应该确定具体的类型,但是当我尝试将InformationBlock转换为TextInformationBlock时,抛出异常.

层次:

public class InformationBlockModel
{
    public virtual InformationBlockType Type { get; set; }
}

public class TextInformationBlockModel : InformationBlockModel
{
    public string Text { get; set; }

    public override InformationBlockType Type { get; set; } = InformationBlockType.Text;
}

public class ImageInformationBlockModel : InformationBlockModel
{
    public override InformationBlockType Type { get; set; } = InformationBlockType.Image;
    public string Name { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

asp.net .net-core asp.net-core

2
推荐指数
1
解决办法
2127
查看次数

标签 统计

.net-core ×1

asp.net ×1

asp.net-core ×1