AspNet核心RC2模型绑定给出空模型

时间:2021-01-01 20:23:18

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究竟有什么问题呢?成功的方法和应该做什么改变来返回正确的结果?

2 个解决方案

#1


2  

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(...)

#2


0  

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.

希望这能有所帮助。

#1


2  

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(...)

#2


0  

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.

希望这能有所帮助。