我刚刚开始使用MVC框架开发 ASP.Net Core 2.0 。
从“查看”页面发布(表单提交)数据时,我遇到了 CustomModelBinder
的问题。
以下是我的观点:
<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>
我的模特:
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.
}
我的控制器:
[HttpPost]
public IActionResult CreateWeb([ModelBinder(BinderType = typeof(CustomModelBinder))]ContentModel item)
{
_contentService.Add(item);
_contentService.SaveChanges();
return View();
}
我的自定义模型Binder类:
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;
}
}
以下是发生的事情,
我可以告诉控制器此 ContentModel 是 VideoModel 。 但是,所有后置值(如Name,ContentType等)都为空。
我曾经在MVC5中执行此操作 Polymorphic model binding 它工作得很好。
我的问题是我错过了一些步骤,还是 .Net 核心与模型绑定有关的新内容?
答案 0 :(得分:0)
好吧,我想你的CustomModelBinder
需要更多的逻辑来从提供者那里获取表单值:
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;
}
}