首页 \ 问答 \ 在ASP.NET MVC 1.0中使用ModelBinder处理无效值(Handling invalid values with ModelBinder in ASP.NET MVC 1.0)

在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
更新时间:2023-08-12 14:08

最满意答案

请参阅此处的类型断言:

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 ...
  • 试试这段代码: 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){}
  • 运行时告诉你实际情况,在你将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 ...
  • 如果将zmq.Socket导出为接口,则无法在此类型上使用instanceof 。 您可以在实现中乱七八糟地查看实际实例化的类,但它会变得混乱且不可靠。 一个选项是检查您期望在对象上存在的几个方法并将其放入类型保护功能中,以便您可以在以后需要时更改它: import * as zmq from 'zmq' function isSocket( sock: any ) : sock is zmq.Socket { var castSock = sock as zmq.Socket; // I ...
  • 在这种情况下,传入的值不可能是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操作码而不是通过方法。 但是,只有少数基本类型,因此创建一个方法来检查每种基本类型的所有可能性并不困难。 一方面注意,因为您的示例特别提到了原始 ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)