在加载 mvc3 之前解析页面上的所有 html 帮助器扩展
我编写了一些扩展方法来扩展 html 帮助器类以进行客户端表单验证。我希望能够做的是让页面加载我的所有元素的数组(javascript),这些元素是我的验证“库”的一部分。
示例:
@Html.VTextBox("blah)
@Html.VDropDownList("bling")
@Html.VTextBox("bloo")
加载页面时,我希望在顶部放置一个 javascript 数组,其中填充如下内容:
errorListArr = new Array("blah","bling","bloo");
目前我正在做的是对每个元素使用 errorListArr.push() 并每次写出脚本标签..显然这不是最优雅的方法。我还想弄清楚如何提前解析整个页面,以便我可以将验证服务器端所需的元素列表放在一起。
请告诉我是否可以做到这一点,并且任何代码片段/示例将不胜感激。
I have written some extension methods that extend the html helper class for client side form validation. What I'd like to be able to do is have the page loaded with an array (javascript) of all of my elements that are part of my validation "library".
example:
@Html.VTextBox("blah)
@Html.VDropDownList("bling")
@Html.VTextBox("bloo")
when the page is loaded, I'd like a javascript array placed at the top filled with something like this:
errorListArr = new Array("blah","bling","bloo");
Currently what I'm doing is using errorListArr.push() for each element and writing out script tags each time ... clearly not the most elegant way to do this. I'd also like to figure out how I can parse the whole page ahead of time so that I can put together my list of elements that I'm going to need to validate server side.
Please let me know if this can be done and any code snippet/samples would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我并不完全清楚你想要做什么,但我想我的理解足以为你指明正确的方向。
首先,MVC 3 中的服务器端验证通常作为对采用模型并验证模型的操作的回发来完成。如果模型无效,您将返回带有(现已验证的)模型的视图,并且该视图将为错误的属性呈现适当的错误消息。
默认情况下,您可以通过将某些属性添加到要为其渲染输入的模型的属性来定义要执行的验证。
更重要的是,MVC 足够智能,可以在用户尝试提交表单时输出客户端 JavaScript 来执行许多此类验证,而实际上根本不需要回发!
请参阅这篇文章 演练如何使用模型验证。
在 MVC 3 中,您还可以选择启用 “unobtrusive javascript”验证,它基本上与平常一样执行相同的客户端验证,但它不是生成一堆内联 JavaScript,而是简单地用某些“data-”属性标记输入元素。然后,几个基于 jquery 的库会扫描页面以查找具有这些标志的元素,并向它们添加适当的验证处理程序。
如果您的验证需求超出了 MVC 提供的开箱即用的范围,则可以轻松添加您自己的验证属性,甚至使您的模型执行自定义逻辑来验证自身。
一般来说,如果您发现在客户端呈现大量小 javascript 片段,您可能需要自己遵循“不显眼的 javascript”模式:
I'm not entirely clear on what you're trying to do, but I think I understand enough to point you in the right direction.
First of all, server-side validation in MVC 3 is typically done as a postback to an action that takes a model and validates it. If the model isn't valid, you return the view with the (now-validated) model, and the view will render the appropriate error messages for the properties that were wrong.
By default, you can define what validation to perform by adding certain attributes to the properties on the model that you're rendering inputs for.
What's more, MVC is smart enough to output client-side javascript to perform many of these validations when the user tries to submit the form, without actually requiring a post-back at all!
See this post for a walk-through of how to use model validation.
In MVC 3, you also have the option to enable "unobtrusive javascript" validation, which basically does the same client-side validation as usual, but instead of generating a bunch of inline javascript, it simply flags the input elements with certain "data-" attributes. A couple of jquery-based libraries then scan the page for elements with these flags, and add the appropriate validation handlers to them.
If you have validation needs that go beyond what MVC offers out of the box, it is easy to add your own validation attributes, or even make your model perform custom logic to validate itself.
Generally speaking, if you find that you're rendering a lot of little javascript snippets client-side, you may want to follow the "unobtrusive javascript" pattern yourself:
您的“验证库”可能不包含这么多扩展方法。因此,在每个数组中放置相同的代码行以将处理后的元素的名称添加到数组中并不是那么不雅。
另一种方法确实是人为地为所有经过验证的元素创建一些通用的东西,这要归功于您的“验证库”:一个通用的标记接口。这个想法是:不要使用元素作为其真实类型的实例。相反,创建这些类型的子类型,这些子类型也仅使用一个
string ID
成员实现接口IValidated
(我想这里您感兴趣的所有元素都已经具有这样的属性感谢它们是其实例的原始类)。然后,完成所需的所有操作后,只需构建一次数组作为将页面发送到客户端之前的结束操作。现在,由于
IValidated
,所有自定义验证的元素都可以被识别,使用这样的单行代码就非常容易了:SubControls
是一种非常方便的扩展方法,可以获取所有子控件,无论其在控件树中的深度如何:当然,您可能需要进行一些调整才能满足您的确切需求,但这本质上就是这个想法。
第二种选择的主要优点是为所有元素提供“共同的东西”,尽管它们都是各种类的实例,否则可能几乎没有共同点。您创建一个特定于您的项目的新功能抽象,稍后您将能够使用特定的扩展方法来扩展
IValidated
(如果需要),例如...Your "validation library" probably doesn't contain so much extension methods. So it would not be so inelegant to place in each of them the same line of code to add the processed element's name into your array.
Another way would be indeed to artificially make something common to all the elements validated thanks to your "validation library" : a common marker interface. The idea is : don't use the elements as instances of their real types. Make instead subtypes of these types which also implement the interface
IValidated
with only onestring ID
member (I suppose here that all the elements you're interested in already have such a property thanks to the original class they're instances of).Then, once you've done all you need, you build your array just once as an ending action before sending the page to the client. Now that all your custom-validated elements are recognizable thanks to
IValidated
, that's pretty easy with a one-liner like this :SubControls
being a very handy extension method to get all the subcontrols whatever their depth in the controls tree :Sure you may have to make a few adaptations to reach your exact needs, but that's essentialy the idea.
This second option has the main advantage to give "something common" to all your elements, despite they're all instances of various classes which otherwise have maybe really few in common. You create a new functional abstraction which is specific to your project and later you'll be able to extend
IValidated
with specific extension methods if you need to, for instance...