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?