AspNet Core RC2模型绑定给出了null模型

时间:2016-06-15 08:52:35

标签: c# asp.net asp.net-mvc .net-core-rc2

我正在使用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));
    }
}

我在这样的控制器动作上使用它:

[HttpPost]
public IActionResult Create([ModelBinder(BinderType = typeof(MovieModelBinder))] MovieViewModel model)
{
    // Code here
}

问题是我每次都得到一个空模型。 ModelBindingResult.Success方法究竟出了什么问题?应该做出哪些更改才能返回正确的结果?

2 个答案:

答案 0 :(得分:2)

我不确定什么是错的,因为在rc2更新后,同样的代码停止了工作。

有关解决方法,只需手动分配:

;WITH YourTable AS (
SELECT *
FROM (VALUES
('text1', '2016-06-01', 8),
('text2', '2016-06-01', 4),
('text1', '2016-06-02', 24),
('text2', '2016-06-02', 8),
('text1', '2016-06-03', 24),
('text2', '2016-06-03', 24),
('text1', '2016-06-04', 6),
('text2', '2016-06-04', 24),
('text1', '2016-06-05', 24),
('text2', '2016-06-05', 24),
('text1', '2016-06-06', 24),
('text2', '2016-06-06', 8),
('text1', '2016-06-07', 24),
('text2', '2016-06-07', 24),
('text1', '2016-06-08', 6),
('text2', '2016-06-08', 24),
('text1', '2016-06-09', 24),
('text2', '2016-06-09', 24)
) as t(name, [date], [value])
), cte AS (
SELECT  *, 
        CASE WHEN [value] IN (LAG([value],1,0) OVER (PARTITION BY name ORDER BY [date]), LEAD([value],1,0) OVER (PARTITION BY name ORDER BY [date])) THEN 1 ELSE 0 END as r
FROM YourTable
)

SELECT  name, 
        ((SUM([value])/24)/3) * 24 as SUM_VALUE
FROM cte
WHERE r = 1 and [value] = 24
GROUP BY name

之前

name    SUM_VALUE
text1   24
text2   48

答案 1 :(得分:0)

这可能不是你问题的答案,但作为一个例子,我在这里发帖

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; 
        }
    }

我在这样的控制器动作上使用它:

 [HttpPost]
        public ActionResult Enter([ModelBinder(typeof(MBUser))] UrlValidation rulValidation)
        {
           // my methods
        }

希望这会有所帮助。