Nullable types in my views result in modelstate errors
I have several nullable types in my views which result in modelstate errors when nothing is filled in. In that case I receive the error message The value '' is not valid for [property]
.
I thought '' would t开发者_如何学编程ranslate to null, but apparently not? I do use custom editor templates which look something like this:
<%@ Control Language="C#" Inherits="System.Web.Mvc.DefaultViewUserControl<float?>" %>
<%: Html.TextBox("", (Model.HasValue ? Model.Value.ToString() : string.Empty)) %>
I decorate the property with a UIHint to indicate this template should be used.
As usual, the problem is something entirely different and of course entirely my own fault. I registered custom model binders for nullable floats, decimals and doubles.... which considered an empty string invalid. I modified this behavior and now it works. Sorry for all the fuss.
精彩评论