ASP.Net MVC 3 JSON Model Binding and server side model validation mixed with client side validation

前端 未结 3 1823
温柔的废话
温柔的废话 2021-01-31 04:52

I\'ve been playing with the new MVC3 Json Model Binding and it\'s quite nice.

Currently, I can post JSON to controller and bind it. Model Validation occurs nicely too.

3条回答
  •  南方客
    南方客 (楼主)
    2021-01-31 05:36

    @Junto and @Jamey777, you both pass the ModelState to your error function but then you use the global variable instead of the parameter.

    and why dont you just use a little linq like

        private Dictionary GetModelStateErrorsAsString()
        {
            return ModelState.Where(x=> x.Value.Errors.Any())
                .ToDictionary(x => x.Key, x => x.Value.Errors.First().ErrorMessage);
        }
    

提交回复
热议问题