MVC4中的Clinetside正则表达式验证

MVC4中的Clinetside正则表达式验证

本文介绍了MVC4中的Clinetside正则表达式验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个带有以下属性的正则表达式的模型:

I have model with couple of regular expression on properties as below:

该视图是动态视图,我从jquery加载其内容,如下所示:

The view is dynamic view, that I am loading its content from jquery as below:

更新:我找到了问题,但没有找到解决方法.因为我正在使用[UIHint("TextBox")]添加一些自定义类和脚本,所以不呈现正则表达式客户端属性.如果删除UIHInt,将生成所有验证属性,而不会出现任何问题.如果我添加UIHInt,则不会生成任何内容.

UPDATE: I found the issue but not the solution. The regular expression client side attributes are not rendered because I am using [UIHint("TextBox")] to add some custom classes and scripts. If I remove UIHInt, all validation attributes are generated without any issue. If I add UIHInt nothing generates.

不确定我的UIHInt编辑器模板出了什么问题.请指教???

Not sure what is wrong in my UIHInt editor template. pls advise???

EditorTemplate/TextBox.cshtml

EditorTemplate/TextBox.cshtml

@Html.TextBoxFor(m => Model, new {@class="txt"})

我的模特:

public partial class PartyRole
{
    [UIHint("TextBox")]
    [RegularExpression(@"^.{5,}$", ErrorMessage="Minimum 5 characters required")]
    [StringLength(50, ErrorMessage="Maximum {2} characters exceeded")]
    public string Title { get; set; }
}

jquery不引人注目地包含在我的捆绑软件中为:

jquery unobtrusive is included in my bundles as:

bundles.Add(new ScriptBundle("~/js/jqueryval").Include(
                    "~/Scripts/jquery.unobtrusive*",
                    "~/Scripts/jquery.validate*"));

查看:

@model dynamic

@using (Html.BeginForm("Edit", null, FormMethod.Post, new { id="FrmIndex" }))
{
@Html.ValidationSummary(true);
@Html.EditorForModel()

 <input type="submit" value="Edit" />
}
<script>
$(".datetime").datetime();
$(".combo").combo();
$(".chk").chk();

var form = $("#FrmIndex")
          .removeData("validator") /* added by the raw jquery.validate plugin */
          .removeData("unobtrusiveValidation");  /* added by the jquery unobtrusive plugin */
$.validator.unobtrusive.parse(form);

</script>

但是仍然不会在客户端生成验证.该消息是从服务器端生成的.

But still the the validations are not generated on client side.The message are generating from server side.

更新:取得了一些突破.在我的强类型视图中,这些验证工作正常.但是,我目前不在这里显示动态视图.

UPDATE: got some break through. These validations are working fine in my strongly typed views. However not in my dynamic view that I am currently showing here.

有人可以告诉我我在这里做什么错吗?

can somebody advise what is wrong I am doing here?

推荐答案

在您的自定义编辑器模板中,您应该欺骗助手以使其位于FormContext内,从而在输入字段上发出HTML5 data- *属性,用于不打扰的验证.因此,在您的模板中放入以下内容,您就可以开始做下去了:

In your custom editor template you should trick the helper into thinking that it is inside a FormContext so that it emits the HTML5 data-* attributes on the input field which are used by unobtrusive validation. So in your template put the following and you are good to go:

@{
    this.ViewContext.FormContext = new FormContext();
}

@Html.TextBox("", ViewData.TemplateInfo.FormattedModelValue, new { @class="txt" })

这篇关于MVC4中的Clinetside正则表达式验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 06:45