2

I have just started to develop ASP.Net Core 2.0 with MVC Framework.

I have some trouble with CustomModelBinder when posting (form submit) data from View page.

Here is my View:

<form action="/Media/CreateVideo" method="post">
                <input type="text" name="Name" />
                <input type="hidden" name="ModelType" value="VideoModel" />
                <input type="text" name="ContentType" value="video" />

                <button type="submit">Yes</button>
  </form>

My models:

    public abstract class ContentModel
    {
        [Key]
        public int Id { get; set; }
        [Required]
        public string Name { get; set; }
        [Required]
        public string ContentType { get; set; }
        public string Data { get; set; }

        public virtual FolderModel ParentFolder { get; set; }
    }



    public abstract class FileModel : ContentModel
    {
        public string Path { get; set; }
    }

    public class VideoModel : FileModel
    {
        //Other properties i.e. video duration, size, format etc.
    }

    public class ImageModel : FileModel
    {
        //Other properties i.e. size, format, cropping value, hue, etc.
    }

My Controller:

        [HttpPost]
        public IActionResult CreateWeb([ModelBinder(BinderType = typeof(CustomModelBinder))]ContentModel item)
        {
            _contentService.Add(item);
            _contentService.SaveChanges();

            return View();
        }

My Custom Model Binder class:

public class CustomModelBinder : IModelBinder
    {

        public Task BindModelAsync(ModelBindingContext bindingContext)
        {
            if (bindingContext == null)
                throw new ArgumentNullException(nameof(bindingContext));

            ValueProviderResult values = bindingContext.ValueProvider.GetValue("ModelType");
            if (values.Length == 0)
                return Task.CompletedTask;

            string typeString = values.FirstValue;
            Type type = Type.GetType(
                "Magic.Core.Models." + typeString + ", Magic.Core.Models",
                true
            );

            object model = Activator.CreateInstance(type);

            var metadataProvider = (IModelMetadataProvider)bindingContext.HttpContext.RequestServices.GetService(typeof(IModelMetadataProvider));
            bindingContext.ModelMetadata = metadataProvider.GetMetadataForType(type);
            bindingContext.Result = ModelBindingResult.Success(model);

            return Task.CompletedTask;
        }
    }

Here is what happens,

I am able to tell the controller that this ContentModel is VideoModel. However, all post value such as Name, ContentType, etc. is all null.

I used to do this in MVC5 following this thread Polymorphic model binding and it just worked fine.

My question is did I miss some steps or there is something new in .Net Core related to model binding?

DemonEden
  • 23
  • 4

1 Answers1

0

well, I guess your CustomModelBinder needs some more logic to get the form values from provider:

public class CustomModelBinder : IModelBinder
{

    public Task BindModelAsync(ModelBindingContext bindingContext)
    {
        if (bindingContext == null)
            throw new ArgumentNullException(nameof(bindingContext));

        ValueProviderResult values = bindingContext.ValueProvider.GetValue("ModelType");
        if (values.Length == 0)
            return Task.CompletedTask;

        string typeString = values.FirstValue;
        Type type = Type.GetType(
            "Magic.Core.Models." + typeString + ", Magic.Core.Models",
            true
        );

        object model = Activator.CreateInstance(type);

        //*get form values from provider
        var content = model as ContentModel;
        if(content != null)
        {
            var provider = bindingContext.ValueProvider;

            var contentType = provider.GetValue("ContentType");
            content.ContentType = contentType != null ? contentType.ToString() : string.Empty;

            var name = provider.GetValue("Name");
            content.Name = name != null ? name.ToString() : string.Empty;
        }
        //*/

        var metadataProvider = (IModelMetadataProvider)bindingContext.HttpContext.RequestServices.GetService(typeof(IModelMetadataProvider));
        bindingContext.ModelMetadata = metadataProvider.GetMetadataForType(type);
        bindingContext.Result = ModelBindingResult.Success(model);

        return Task.CompletedTask;
    }
}
Bob Dust
  • 2,370
  • 1
  • 17
  • 13
  • Still wonder why I have to bind the post data by myself. But in MVC5 it automatically do this for me? – DemonEden Jan 18 '18 at 06:44
  • @DemonEden, you're probably recalling `DefaultModelBinder` of ASP NET (without Core) MVC? – Bob Dust Jan 18 '18 at 09:40
  • Yes, the DefaultModelBInder and its CreateModel Method. But now, I ended up doing something like this PropertyInfo[] infoList = type.GetProperties(); var provider = bindingContext.ValueProvider; foreach (PropertyInfo info in infoList) { var val = provider.GetValue(info.Name).FirstValue; info.SetValue(model, val, null); } – DemonEden Jan 18 '18 at 10:53
  • Using reflection needs considering due to its impact on performance. – Bob Dust Jan 18 '18 at 11:01