在 MVC3 中扩展不显眼的 javascript 以将样式添加到 div 客户端的最佳方法
我正在使用 html5/Razor/MVC3,利用 Twitter 的 Bootstrap 模板。我希望表单验证看起来像他们记录的那样光滑(http://twitter.github.com/bootstrap/#forms )。因此,如果我们看一下用于帐户注册的标准样板 MVC3,标记将如下所示:
@using (Html.BeginForm("Register", "Account", FormMethod.Post, new { @class="form-stacked" })) {
@Html.ValidationSummary(true, "Snap! Something went wrong")
<div>
<fieldset>
<legend>Account Information</legend>
<div class="clearfix error">
@Html.LabelFor(m => m.UserName)
<div class="input">
@Html.TextBoxFor(m => m.UserName)
<span class="help-inline">@Html.ValidationMessageFor(m => m.UserName)</span>
</div>
</div>
<div class="clearfix">
@Html.LabelFor(m => m.Email)
<div class="input">
@Html.TextBoxFor(m => m.Email)
<span class="help-inline">@Html.ValidationMessageFor(m => m.Email)</span>
</div>
</div>
<div class="clearfix">
@Html.LabelFor(m => m.Password)
<div class="input">
@Html.PasswordFor(m => m.Password)
<span class="help-inline">@Html.ValidationMessageFor(m => m.Password)</span>
</div>
</div>
<div class="clearfix">
@Html.LabelFor(m => m.ConfirmPassword)
<div class="input">
@Html.PasswordFor(m => m.ConfirmPassword)
<span class="help-inline">@Html.ValidationMessageFor(m => m.ConfirmPassword)</span>
</div>
</div>
</fieldset>
<div class="actions">
<button class="btn large primary" type="submit">Register</button>
</div>
</div>
我想要做的是让容器 div 注入“错误”类,就像我在第一个中硬编码的那样输入。 (因此,进入页面后,div 将具有“clearfix”类,但如果该输入块验证失败,它会将其标记为“clearfix 错误”)。我想我必须更新 div 块以包含某种 id,并且可能向 ValidationMessage 添加一个新的 data- 属性。我在扩展 ValidationMessageFor 帮助程序时没有问题。我只是不能 100% 确定扩展现有库的方法应该是什么。关于如何解决这个问题有什么建议吗?
TIA。
更新:
我认为这种方法是合理的:
<div id="UserNameContainer" class="clearfix error">
@Html.LabelFor(m => m.UserName)
<div class="input">
@Html.TextBoxFor(m => m.UserName)
<span class="help-inline">@Html.ValidationMessageFor(m => m.UserName, null, new { @data_container = "UserNameContainer" })</span>
</div>
</div>
通过用数据容器名称装饰我的验证消息,我就可以定位容器 div。现在我只需要弄清楚如何拦截验证消息。
I'm using html5/Razor/MVC3 leveraging the Bootstrap template from Twitter. I want to have form validation that looks slick like they've documented (http://twitter.github.com/bootstrap/#forms). So if we take a look at how the standard boiler-plate MVC3 for account registration, the markup would look like:
@using (Html.BeginForm("Register", "Account", FormMethod.Post, new { @class="form-stacked" })) {
@Html.ValidationSummary(true, "Snap! Something went wrong")
<div>
<fieldset>
<legend>Account Information</legend>
<div class="clearfix error">
@Html.LabelFor(m => m.UserName)
<div class="input">
@Html.TextBoxFor(m => m.UserName)
<span class="help-inline">@Html.ValidationMessageFor(m => m.UserName)</span>
</div>
</div>
<div class="clearfix">
@Html.LabelFor(m => m.Email)
<div class="input">
@Html.TextBoxFor(m => m.Email)
<span class="help-inline">@Html.ValidationMessageFor(m => m.Email)</span>
</div>
</div>
<div class="clearfix">
@Html.LabelFor(m => m.Password)
<div class="input">
@Html.PasswordFor(m => m.Password)
<span class="help-inline">@Html.ValidationMessageFor(m => m.Password)</span>
</div>
</div>
<div class="clearfix">
@Html.LabelFor(m => m.ConfirmPassword)
<div class="input">
@Html.PasswordFor(m => m.ConfirmPassword)
<span class="help-inline">@Html.ValidationMessageFor(m => m.ConfirmPassword)</span>
</div>
</div>
</fieldset>
<div class="actions">
<button class="btn large primary" type="submit">Register</button>
</div>
</div>
What I want to do is have the container div inject the "error" class like I've hard-coded in the first input. (So upon entering the page, the div would have a class of "clearfix" but if that input block failed validation, it would tag it as "clearfix error"). I figure I'm going to have to update the div block to include an id of some sort and perhaps add a new data- attribute to the ValidationMessage. I don't have a problem extending the ValidationMessageFor helper. I'm just not 100% sure what the approach should be for extending the library that's there. Any suggestions on how to approach this?
TIA.
UPDATE:
I am thinking this approach is reasonable:
<div id="UserNameContainer" class="clearfix error">
@Html.LabelFor(m => m.UserName)
<div class="input">
@Html.TextBoxFor(m => m.UserName)
<span class="help-inline">@Html.ValidationMessageFor(m => m.UserName, null, new { @data_container = "UserNameContainer" })</span>
</div>
</div>
By decorating my validation message with a data-container name, I could then target the container div. Now I just need to figure out how to intercept the validation message.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
$.validator.setDefaults
方法通过 Twitter 的 Bootstrap 为我解决了这个问题。我正在使用jquery.validate.js
和jquery.validate.unobtrusive.js
。由于 DOM 就绪的非侵入式验证会扫描您的文档并为其遇到的每个表单缓存非侵入式验证选项,因此需要在文档扫描发生之前调用
$.validator.setDefaults
方法。The
$.validator.setDefaults
method solved this issue for me with Bootstrap from Twitter. I'm usingjquery.validate.js
andjquery.validate.unobtrusive.js
.Since unobtrusive validation on DOM ready scans your document and caches unobtrusive validation options for each form it encounters, it is needed to call the
$.validator.setDefaults
method before document scan occurs.遇到了同样的问题。我通过添加和扩展 HtmlHelper 类来解决这个问题。
这就是我对 ValidationSummary 所做的:
在 .cshtml 文件中,我将 Html.ValidationSummary 替换为:
请记住在视图文件夹 web.config 文件中添加扩展类的命名空间。
如果我先解决您之前的单个输入项目,我会稍后在这里发布。
华泰
Came accross the same issue. I am tackling it by adding and extesion to the HtmlHelper Class.
This is what I did for the ValidationSummary:
And in the .cshtml file I replace Html.ValidationSummary with this:
Remember to add the namespance of your extension class in the views folder web.config file.
I will post here later if I tackle the individual input item before you.
HTH
不要重新发明这个特定的轮子,而是检查
您可以根据需要自定义弹出元素,连接到 jQuery.validate.js 也很简单。
Rather than reinventing this particular wheel, check the validationEngine plugin available at http://www.position-absolute.com/articles/jquery-form-validator-because-form-validation-is-a-mess/.
You can customize the popup elements as you want, and it is trivial to connect to jQuery.validate.js.
我更喜欢更改 bootstrap 的 CSS。
刚刚在正确的位置添加了 jQuery 验证类。
字段验证错误和输入验证错误
I prefere to change the CSS of bootstrap.
Just added the classes of jQuery validate in the right place.
field-validation-error and input-validation-error
您可以通过将以下 javascript 添加到页面(视图)来将 MVC3 验证与 Bootstrap 框架集成。
此外,在视图(例如“Create.cshtml”)上,确保表单中的字段格式如下
...对于这个解决方案,很可能只需添加 JavaScript 即可,无需编辑视图。
You can integrate MVC3 validation with Bootstrap framework by adding the following javascript to your page (View)
Besides, on the Views (for example "Create.cshtml") make sure that the fields in the form are formatted as the following...
With this solution, it will most likely be enough to just add a javascript without edit the View.
我所做的就是采用 css 类来处理验证错误,并创建一个具有相同类但具有引导值的新 css 文件。
您可以在 nuget 包中找到它: http://nuget.org/List/Packages /MahApps.Twitter.Bootstrap
它还提供了一些脚手架模板来自动创建新视图。
What I've done is taken the css classes for the validation errors and created a new css file with the same classes but with bootstrap values.
You can find it in a nuget package at: http://nuget.org/List/Packages/MahApps.Twitter.Bootstrap
That also provides some scaffolding templates to autocreate new views.
我需要使用 Bootstrap 3.1 和 Razor 来解决这个问题。这就是我使用的:
这是@german的答案和帮助的组合 帖子,作者:“theBraindonor”。更新为使用新的 Bootstrap 3 类。
I needed to solve this using Bootstrap 3.1 and Razor. This is what I used:
This is a combination of @german's answer and help from this post by "theBraindonor". Updated to use new Bootstrap 3 classes.