MVC 2,自定义ModelBinder,并存储“尝试的值”为了景观
我有一个对象,以前可以很好地从服务器到客户端往返(即编辑和查看表单工作正常)。但是,我已向该对象添加了一个复杂类型,并创建了一个自定义 ModelBinder 来处理它。复杂类型可以表示为单个字符串,但以 Decimal 形式存储在对象中(认为“0 ft. 3in.”可以存储为 0.25)。
然而,我的问题是验证失败。如果用户键入“asdf”,验证将失败。但是 Decimal 中存储的只是 0。因此,当渲染视图并将其发送给用户时,他们只会看到“0”和错误消息,而不是看到“asdf”。我的默认 ModelBinder 没有这个问题(其他字段往返无效字符串就好了),所以我一定在我的自定义 ModelBinder 中遗漏了一些东西......但它是什么?
我尝试过打电话
bindingContext.ModelState.SetModelValue(bindingContext.ModelName, New ValueProviderResult(null, attemptedValue, Globalization.CultureInfo.CurrentCulture))
,但似乎没有任何影响。
I have an object that was previously round-tripping from the server to the client and back nicely (i.e. edit and view forms worked fine). However, I've added to that object a complex type and made a custom ModelBinder to handle it. The complex type can be represented as a single string but is stored in the object as a Decimal (think "0 ft. 3in." can be stored as 0.25).
My problem, however, is when validation fails. If a user types "asdf", validation fails. But what gets stored in the Decimal is just 0. So when the view is rendered and sent to the user, they just see "0" and an error message, rather than seeing "asdf". I don't have this problem with the default ModelBinder (other fields round-trip the invalid string just fine), so I must be missing something in my custom ModelBinder... but what is it?
I've tried calling
bindingContext.ModelState.SetModelValue(bindingContext.ModelName, New ValueProviderResult(null, attemptedValue, Globalization.CultureInfo.CurrentCulture))
But it doesn't seem to affect anything.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
StackOverflow 上没有答案。在 ASP.NET 论坛上得到答案: http://forums.asp.net/ p/1571473/3941396.aspx#3941396
No answers on StackOverflow. Got answer on ASP.NET forums: http://forums.asp.net/p/1571473/3941396.aspx#3941396