MVC3剃刀局部视图渲染不包括数据 - 验证属性 [英] MVC3 Razor Partial view render does not include data- validation attributes

查看:151
本文介绍了MVC3剃刀局部视图渲染不包括数据 - 验证属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有个人的呈现数据作为形式的中心部分视图farily直接的形式。我不能让客户端验证这种形式的合作。
我开始追逐下生成html,并呈现一个标准的形式和局部视图在同一个模型外地赶来了。

I have a farily straight forward form that renders personal data as a partial view in the center of the form. I can not get client side validation to work on this form. I started chasing down the generate html and came up with the same model field rendered on a standard form and a partial view.

我注意到,输入元素正确填充在第一次通话,@ html.partial,下面只发生在partialview通过一个Ajax请求加载。

I noticed that the input elements are correctly populated on the first call, @html.partial, the following only happens when the partialview is reloaded via an ajax request.

首先我局部视图的标题,这是主要的页面上Ajax.BeginForm内。

First the header of my partial view, this is within a Ajax.BeginForm on the main page.

@model MvcMPAPool.ViewModels.EventRegistration
<script src="@Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")"         type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function ()
{
    $(".phoneMask").mask("(999) 999-9999");
});
</script>

@{
    var nPhn = 0;
    var dTotal = 0.0D;
    var ajaxOpts = new AjaxOptions{ HttpMethod="Post", UpdateTargetId="idRegistrationSummary", OnSuccess="PostOnSuccess" };
    Html.EnableClientValidation( true );
    Html.EnableUnobtrusiveJavaScript( true );
}

下面是从局部视图剃刀标记:

Here is the razor markup from the partial view:

@Html.ValidationMessageFor(model=>Model.Player.Person.Addresses[0].PostalCode)
<table>
    <tr>
        <td style="width:200px;">City*</td>
        <td>State</td>
        <td>Zip/Postal Code</td>
    </tr>
    <tr>
        <td>@Html.TextBoxFor(p=>Model.Player.Person.Addresses[0].CityName, new { style="width:200px;", maxlength=50 })</td>
        <td>
        @Html.DropDownListFor(p=> Model.Player.Person.Addresses[0].StateCode
                                 , MPAUtils.GetStateList(Model.Player.Person.Addresses[0].StateCode))</td>
        <td>
        <div class="editor-field">
        @Html.TextBoxFor(p=>Model.Player.Person.Addresses[0].PostalCode, new { style="width:80px;", maxlength=10 })
        </div>
        </td>
    </tr>
</table>

下面是从局部视图渲染领域:

Here is the rendered field from the partial view:

        <td>
        <div class="editor-field">
        <input id="Player_Person_Addresses_0__PostalCode" maxlength="10" name="Player.Person.Addresses[0].PostalCode" style="width:80px;" type="text" value="" />
        </div>
        </td>

下面是一个标准视图渲染同型号场:

Here is the same model field rendered in a standard view:

        <div class="editor-field">            
            <input data-val="true" data-val-length="The field Postal/Zip Code must be a string with a maximum length of 10." data-val-length-max="10" data-val-required="Postal or Zip code must be provided!" id="Person_Addresses_0__PostalCode" maxlength="10" name="Person.Addresses[0].PostalCode" title="Postal/Zip Code is required" type="text" value="" />       
            <span class="field-validation-valid" data-valmsg-for="Person.Addresses[0].PostalCode" data-valmsg-replace="true"></span>        
        </div>

注意局部视图渲染具有输入元素上没有数据-VAL-XXX的属性。

Notice that the partial view rendering has no data-val-xxx attributes on the input element.

这是正确的?我没有看到客户端验证如何没有这些属性的工作,还是我失去了一些东西基本在这里?

Is this correct? I do not see how the client side validation could work without these attributes, or am I missing something basic here?

推荐答案

为了创建不引人注目的验证属性,一个 FormContext 必须存在。添加下面为您局部视图的顶部:

In order to create the unobtrusive validation attributes, a FormContext must exist. Add the following at the top of your partial view:

if (this.ViewContext.FormContext == null) 
{
    this.ViewContext.FormContext = new FormContext(); 
}

这篇关于MVC3剃刀局部视图渲染不包括数据 - 验证属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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