在ASP.NET MVC 1.0中使用ModelBinder处理无效值(Handling invalid values with ModelBinder in ASP.NET MVC 1.0)
首先是一些背景 :
我有一个表单,在那里我发布了一些由MVC内置的ModelBinder自动实现对象的对象:
<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);
另外:我想强调的是,我们正在向DTO(DataTransferObjects)发布我们进一步沿着线图映射到实体框架实体,所以我们宁愿不喜欢DTO上的任何更改,除非可能添加属性。
问题
如果用户为“小时”输入无效值,例如“Fubar”,则ModelBinder自然不会尝试设置“小时”属性。 但它默认为0,因为它是一个int而不是一个字符串 。
这对我来说会造成一些困难,因为现在我无法看到用户是否实际输入了0,或者这是否是由无效输入引起的。
由于我使用的是自制的对象到实体(Entity Framework)映射器,我们无法将'Hours'属性的脚印更改为int? 。 我知道MVC有一些内置的验证,但我们宁愿不实现它,因为我们知道它已经受到了极大的攻击,并且ASP.NET MVC 2.0中还有一些新的验证。
解?
我需要能够向用户指出哪个字段不正确,这意味着我需要能够捕获异常(或者可能是其他一些巧妙的解决方案?),在那里我可以做一些逻辑并回发一个新的查看用户我澄清他们做错了什么。
我目前的想法:编写自定义ModelBinder。
你有什么建议?
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:
<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.
Problem
If a user enters an invalid value for "Hours", say 'Fubar', then the ModelBinder naturally will not attempt to set the 'Hours'-property. But it defaults to 0, because it is an int and not a 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.
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?. 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.
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.
What do you suggest?
原文:https://stackoverflow.com/questions/2134100
最满意答案
请参阅此处的类型断言:
http://golang.org/ref/spec#Type_assertions
我只会断言一个明智的类型(字符串,uint64)等,并尽可能保持松散,最后执行到本机类型的转换。
See type assertions here:
http://golang.org/ref/spec#Type_assertions
I'd assert a sensible type (string, uint64) etc only and keep it as loose as possible, performing a conversion to the native type last.
相关问答
更多-
请参阅此处的类型断言: http://golang.org/ref/spec#Type_assertions 我只会断言一个明智的类型(字符串,uint64)等,并尽可能保持松散,最后执行到本机类型的转换。 See type assertions here: http://golang.org/ref/spec#Type_assertions I'd assert a sensible type (string, uint64) etc only and keep it as loose as possib ...
-
在你的代码中,你可以依靠这样一个事实,即你所递交的类型与他们的声明方式相对应。 如果情况并非如此,你不可能写出任何不平凡的程序。 C中的类型信息仅在编译时才可用。 在运行时,这些信息都不存在,因此没有标准的内置方式,例如,告诉什么类型的对象隐藏在随机指针后面。 如果你需要这样的信息,看看你的编译器是否有扩展(我不知道是否有),或者使用为其提供基础设施的框架( glib拥有我相信的东西)。 或者如果你确实需要的话,请自行推出。 或者使用确实有一些运行时类型信息支持的C ++,并且通常来说是一个更复杂的类型系统 ...
-
因此,严格来说,“变量类型”始终存在,并可作为类型参数传递。 例如: val x = 5 def f[T](v: T) = v f(x) // T is Int, the type of x 但是,根据你想做什么 ,这不会帮助你。 例如,可能不想知道变量的类型,但是要知道值的类型是否是某种特定类型,例如: val x: Any = 5 def f[T](v: T) = v match { case _: Int => "Int" case _: String => "String" ca ...
-
Java:在运行时检查变量类型的最佳方法是什么?(Java : What is the best way to check variable type in runtime?)[2022-06-23]
试试这段代码: if(floatVariable instanceof Float){} if(intVariable instanceof Integer){} if(stringVariable instanceof String){} try this code : if(floatVariable instanceof Float){} if(intVariable instanceof Integer){} if(stringVariable instanceof String){} -
Visual C ++ - 运行时检查失败#3 - 变量未初始化(Visual C++ - Runtime Check Failure #3 - Variable is not initiliazed)[2023-11-16]
运行时告诉你实际情况,在你将number声明为int但未给它赋值之后会出现以下行。 cout << "Enter " << number << " Value Name: "; 在您的代码中,您声明以下内容,在C ++中这意味着给我2个整数,但是值尚未定义,例如 int offset, number; 把它改成这样的东西.. int offset = 0; int number = 0; The runtime is telling you the truth, the following line ... -
如何在运行时检查此TypeScript代码中的instanceof?(How can I check instanceof in this TypeScript code at runtime?)[2022-03-08]
如果将zmq.Socket导出为接口,则无法在此类型上使用instanceof 。 您可以在实现中乱七八糟地查看实际实例化的类,但它会变得混乱且不可靠。 一个选项是检查您期望在对象上存在的几个方法并将其放入类型保护功能中,以便您可以在以后需要时更改它: import * as zmq from 'zmq' function isSocket( sock: any ) : sock is zmq.Socket { var castSock = sock as zmq.Socket; // I ... -
在运行时知道类型变量(knowing type variable at runtime)[2023-09-28]
在这种情况下,传入的值不可能是GUID。 GUID结构是一个值类型 - 它不能被继承。 这意味着只有类型编译器才会接受作为此方法的参数是一个GUID 。 In this case, there is no chance that the passed in value is not a GUID. The GUID structure is a value type - it can't be inherited. This means that only type the compiler will ac ... -
是的,就这样做 mov eax, 4 ; eax = size of ebx in bytes CPU寄存器的大小不灵活,它们是固定的。 至于变量,只需在变量之后添加一个标签并使用差异,如下所示: mystring db "blah blah blah",10,0 mystringend label $ ; or simply "mystringend:" ... mov eax, mystringend - mystring ; or "offset mystringend - offset mystr ...
-
您可以将一个抽象方法( tATag )添加到您的Service特征中,该特征返回TA的TypeTag,在ServiceA1和ServiceA2执行此操作(btw您的服务应该扩展Service特征)并在比较时使用它,如下所示: trait Service { type tA <: A def ping(a:tA) def tATag: TypeTag[tA] } class ServiceA1 extends Service { type tA = A1 def ping(a:tA){p ...
-
对于非基本类型,您可以反映并检查是否在支持转换的任一类型上都有op_Implicit方法。 IL实际上并不支持真正的运算符重载,因此它纯粹是C#的常规系统,用于识别运算符重载。 如果它是从C#中的运算符重载定义创建的,则该方法也将标记为IsSpecialName。 对于原始类型 (如Int32和Int64),最简单的选择是对各种情况进行硬编码,因为转换是通过原始IL操作码而不是通过方法。 但是,只有少数基本类型,因此创建一个方法来检查每种基本类型的所有可能性并不困难。 一方面注意,因为您的示例特别提到了原始 ...