Asp.Net MVC 3客户端验证,属性代 [英] Asp.Net Mvc 3 Client Validation, Attributes generation

查看:135
本文介绍了Asp.Net MVC 3客户端验证,属性代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Asp.net MVC3广告的一些自定义属性一样来执行验证数据-VAL-需要上输入元素。我知道这一切背后的理论,它是如何工作的。

Asp.net Mvc3 ads some custom attributes like "data-val-required" on input elements to perform validation. I know all theory behind this, how it works.

我想知道的是:

在我里面创建我的表@using(Html.BeginForm()),它产生的自定义属性,但是当我把普通的我的形式,它不会创建这些属性的<形式为GT ; 标签

When I create my form inside " @using (Html.BeginForm())" it produces custom attributes, but it doesn't create those attributes when i place my form between plain "<form>" tag.

下面是我创建演示一下亚姆说一个演示

below is a demo i have created to demonstrate what iam saying

剃须刀code,里面BefingForm()形式

 @using (Html.BeginForm()) {
                @Html.EditorFor(model => model.EmailAddress)
                @Html.ValidationMessageFor(model => model.EmailAddress)
}

生成的HTML 包含以下

<input type="text" value=""  data-val-required="The Email Address field is required."  data-val-email="my message">

纯HTML标签内的剃须刀code组成

<form action="/Account/Register" method="post">
            @Html.EditorFor(model => model.EmailAddress)
            @Html.ValidationMessageFor(model => model.EmailAddress)
</form>

生成的HTML 犯规包含数据-VAL-需要如下图所示。

generated Html doesnt contain "data-val-required" attribute shown below

<input type="text" value=""  gtbfieldid="44">

我的问题是我怎么能问MVC来添加表格放在一边纯HTML标记这些属性。

My question is how can i ask MVC to add those attributes even form is placed in side pure html tags

推荐答案

我相信BeginForm方法在内部分配formcontext对象viewCotnext的财产FormContext。如果你不希望使用普通的HTML表单标签,你必须这样做手工像

I believe BeginForm method internally assigns a formcontext object to viewCotnext's property FormContext. If you do not want to use plain html form tags you have to do it manually like

<%
    this.ViewContext.FormContext = new FormContext();
%>

在剃刀它很可能是

and in razor it would probably be

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

这篇关于Asp.Net MVC 3客户端验证,属性代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆