[英]AspNet Core RC2 Model binding gives null model
I am working with ASP.NET CORE RC2 and I have the following model binder: 我正在使用ASP.NET CORE RC2,我有以下模型绑定器:
public class MovieModelBinder : IModelBinder
{
public Task BindModelAsync(ModelBindingContext bindingContext)
{
if (bindingContext.ModelType == typeof(MovieViewModel))
{
var idValue = bindingContext.ValueProvider.GetValue("Id").FirstValue;
var nameValue = bindingContext.ValueProvider.GetValue("Name").FirstValue;
var timespanProperty = bindingContext.ModelMetadata.Properties.Single(p => p.PropertyName == "Length");
var timespanValue = bindingContext.ValueProvider.GetValue(timespanProperty.PropertyName).FirstValue;
int minutes;
int.TryParse(timespanValue, out minutes);
int id;
int.TryParse(idValue, out id);
var model = new MovieViewModel
{
Length = TimeSpan.FromMinutes(minutes),
Id = id,
Name = nameValue
};
return Task.FromResult(ModelBindingResult.Success(bindingContext.ModelName, model));
}
return Task.FromResult(default(ModelBindingResult));
}
}
I am using it on a controller action like this: 我在这样的控制器动作上使用它:
[HttpPost]
public IActionResult Create([ModelBinder(BinderType = typeof(MovieModelBinder))] MovieViewModel model)
{
// Code here
}
Problem is that I get a null model every time. 问题是我每次都得到一个空模型。 What is exactly wrong with the ModelBindingResult.Success method and what change should be made to return correct results?
ModelBindingResult.Success方法究竟是什么问题,应该做出哪些更改才能返回正确的结果?
I'm not sure whats wrong, for me same code stopped working after rc2 update. 我不确定什么是错的,因为同样的代码在rc2更新后停止工作。
For workaround just manually assign : 要解决方法,只需手动分配:
bindingContext.Result = ModelBindingResult.Success(bindingContext.ModelName, model);
before 之前
return Task.FromResult(...)
This maybe not a Answer to your question but as an example I am posting it here 这可能不是你的问题的答案,但作为一个例子,我在这里发布它
This ModelBinder
example bind the posted values to my UrlValidation
Type 此
ModelBinder
示例将发布的值绑定到我的UrlValidation
类型
public class MBUser : IModelBinder
{
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
HttpContextBase httpContextBase = controllerContext.RequestContext.HttpContext;
UrlValidation urlValidation = new UrlValidation();
//Binding posted values to UrlValidation type
urlValidation.Expirydate = DateTime.ParseExact(
httpContextBase.Request["UrlValidation.Expirydate"].ToString(),
"d/M/yyyy", CultureInfo.InvariantCulture).ToString("G");
urlValidation.ProjectTypeID = Convert.ToInt16(httpContextBase.Request["ProjectType"]);
urlValidation.Url = httpContextBase.Request["UrlValidation.Url"].ToString();
string datetime = DateTime.Now.ToString("G");
urlValidation.CreateDate = datetime;
// returning UrlValidation type
return urlValidation;
}
}
I am using it on a controller action like this: 我在这样的控制器动作上使用它:
[HttpPost]
public ActionResult Enter([ModelBinder(typeof(MBUser))] UrlValidation rulValidation)
{
// my methods
}
Hope this will help somehow. 希望这会有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.