简体   繁体   English

在ASP.NET MVC 1.0中使用ModelBinder处理无效值

[英]Handling invalid values with ModelBinder in ASP.NET MVC 1.0

First of all some context : 首先一些背景

I have a form, where I post back some objects that are automatically materialized into objects by MVCs built-in ModelBinder: 我有一个表单,其中回发了一些由内置ModelBinder的MVC自动实现为对象的对象:

<input type="hidden" name="myobj[0].Id" />
<input type="text" name="myobj[0].Hours" />
<input type="hidden" name="myobj[1].Id" />
<input type="text" name="myobj[1].Hours" />

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Save(IList<MyObject> myobj);

Additionally: I would like to stress that we are posting to DTO (DataTransferObjects) that we further down the line map into entity framework entities, so we would rather not like the change anything on the DTO's except maybe adding attributes. 另外:我想强调一点,我们要在DTO(DataTransferObjects)上发布进一步将线图向下映射到实体框架实体的方法,因此我们不希望在DTO上进行任何更改,除非添加属性。

Problem 问题

If a user enters an invalid value for "Hours", say 'Fubar', then the ModelBinder naturally will not attempt to set the 'Hours'-property. 如果用户为“小时”输入了无效值,例如说“ Fubar”,则ModelBinder自然不会尝试设置“小时”属性。 But it defaults to 0, because it is an int and not a string . 但是它默认为0,因为它是一个int而不是string

This causes some difficulty for me ofcourse, because now I can't see if the user actually entered 0, or if this was caused by invalid input. 当然,这给我带来了一些困难,因为现在我看不到用户是否实际输入了0,或者这是由无效输入引起的。

Since I am using a home-rolled object-to-entity (Entity Framework) mapper, we cannot change the foot-print of the 'Hours'-property to int? 由于我使用的是自卷式对象到实体(实体框架)映射器,因此我们无法将“小时”属性的足迹更改为int吗? . I am aware that MVC has some built in Validation, but we would rather not implement that since we know it has been wildly attacked and that there is some new validation coming in ASP.NET MVC 2.0. 我知道MVC具有一些内置的Validation,但是我们宁愿不实施它,因为我们知道它受到了广泛的攻击,并且ASP.NET MVC 2.0中引入了一些新的验证。

Solution? 解?

I need to be able to point out to the user which field is incorrect, so that means I somehow need to be able to catch an exception (or possibly some other ingenious solution?), where I can do some logic and post back a new view to the user where I clarify what they did incorrectly. 我需要向用户指出哪个字段不正确,这意味着我需要以某种方式捕获异常(或者可能是其他一些巧妙的解决方案?),在这里我可以执行一些逻辑并回发新的查看用户向我说明他们做错了什么。

My current idea: Writing a custom ModelBinder. 我当前的想法:编写自定义ModelBinder。

What do you suggest? 你有什么建议?

This causes some difficulty for me ofcourse, because now I can't see if the user actually entered 0, or if this was caused by invalid input. 当然,这给我带来了一些困难,因为现在我看不到用户是否实际输入了0,或者这是由无效输入引起的。

Yes you can. 是的你可以。 Look in the ModelState. 查看ModelState。

While this is not particularly ideal, you've stated that you don't want to change anything else. 尽管这并不是特别理想,但您已经声明不想更改其他任何内容。 So the option you mentioned, creating a custom ModelBinder will work fine. 因此,您提到的创建一个自定义ModelBinder的选项可以正常工作。 However, you might find yourself creating a ModelBinder for each entity that you want to bind. 但是,您可能会发现要为每个要绑定的实体创建一个ModelBinder。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM