没有验证属性的部分视图(ASP.NET MVC 3)

时间:2011-11-15 13:54:20

标签: asp.net-mvc asp.net-mvc-3 asp.net-mvc-validation asp.net-mvc-templates

我的一个ASP.NET MVC 3应用程序中发生了一件奇怪的事情。

我通过jQuery Ajax api获取插入行,没有问题。但是当我得到必要的部分视图时,它没有验证属性,我无法重新绑定这些行的验证。

以下是我作为ajax回应得到的内容:

<input type="hidden" name="accommPropertyPeriods.index" autocomplete="off" value="ccaa15b3-76f1-4215-8bb5-a62d700bfc1e" />
    <table style="width:100%;">
    <tr>
        <td>
            <div class="editor-field">
                <select class="chzn-select-deselect" data-placeholder="Choose an Alias..." id="accommPropertyPeriods_ccaa15b3-76f1-4215-8bb5-a62d700bfc1e__AccommPropertySeasonPeriodAliasID" name="accommPropertyPeriods[ccaa15b3-76f1-4215-8bb5-a62d700bfc1e].AccommPropertySeasonPeriodAliasID" style="min-width:100px;"><option value="302">A</option>
<option value="303">B</option>
<option value="304">C</option>
<option value="305">D</option>
</select>

            </div>
        </td>
        <td>
            <div class="editor-field">
                <input class="datefield" id="accommPropertyPeriods_ccaa15b3-76f1-4215-8bb5-a62d700bfc1e__PeriodStartsAt" name="accommPropertyPeriods[ccaa15b3-76f1-4215-8bb5-a62d700bfc1e].PeriodStartsAt" type="text" value="" />

            </div>
        </td>
        <td>
            <div class="editor-field">
                <input class="datefield" id="accommPropertyPeriods_ccaa15b3-76f1-4215-8bb5-a62d700bfc1e__PeriodEndsAt" name="accommPropertyPeriods[ccaa15b3-76f1-4215-8bb5-a62d700bfc1e].PeriodEndsAt" type="text" value="" />

            </div>
        </td>
    </tr>   
    </table>

这是我应该得到的:

<input type="hidden" name="accommPropertyPeriods.index" autocomplete="off" value="84ddd0f5-a3e2-4f10-8e67-f32528c6393d" />
    <table style="width:100%;">
    <tr>
        <td>
            <div class="editor-field">
                <select class="chzn-select-deselect" data-placeholder="Choose an Alias..." data-val="true" data-val-number="The field AccommPropertySeasonPeriodAliasID must be a number." data-val-required="The AccommPropertySeasonPeriodAliasID field is required." id="accommPropertyPeriods_84ddd0f5-a3e2-4f10-8e67-f32528c6393d__AccommPropertySeasonPeriodAliasID" name="accommPropertyPeriods[84ddd0f5-a3e2-4f10-8e67-f32528c6393d].AccommPropertySeasonPeriodAliasID" style="min-width:100px;"><option value="302">A</option>
<option value="303">B</option>
<option value="304">C</option>
<option value="305">D</option>
</select>
                <span class="field-validation-valid" data-valmsg-for="accommPropertyPeriods[84ddd0f5-a3e2-4f10-8e67-f32528c6393d].AccommPropertySeasonPeriodAliasID" data-valmsg-replace="false">*</span>
            </div>
        </td>
        <td>
            <div class="editor-field">
                <input class="datefield" data-val="true" data-val-required="The PeriodStartsAt field is required." id="accommPropertyPeriods_84ddd0f5-a3e2-4f10-8e67-f32528c6393d__PeriodStartsAt" name="accommPropertyPeriods[84ddd0f5-a3e2-4f10-8e67-f32528c6393d].PeriodStartsAt" type="text" value="" />
                <span class="field-validation-valid" data-valmsg-for="accommPropertyPeriods[84ddd0f5-a3e2-4f10-8e67-f32528c6393d].PeriodEndsAt" data-valmsg-replace="false">*</span>
            </div>
        </td>
        <td>
            <div class="editor-field">
                <input class="datefield" data-val="true" data-val-required="The PeriodEndsAt field is required." id="accommPropertyPeriods_84ddd0f5-a3e2-4f10-8e67-f32528c6393d__PeriodEndsAt" name="accommPropertyPeriods[84ddd0f5-a3e2-4f10-8e67-f32528c6393d].PeriodEndsAt" type="text" value="" />
                <span class="field-validation-valid" data-valmsg-for="accommPropertyPeriods[84ddd0f5-a3e2-4f10-8e67-f32528c6393d].PeriodEndsAt" data-valmsg-replace="false">*</span>
            </div>
        </td>
    </tr>   
    </table>
  

GUID不必相同。我正在做所谓的非顺序绑定。

这是我通过jquery ajax调用以获取新插入行的操作:

    [HttpPost]
    public PartialViewResult accommPropertySeasonPeriodCreatePartialView(int id, int subid) {

        //some other stuff going on here. non-related to partial view.

        return PartialView("_AccommPropertySeasonPeriodCreatePartialView");
    }

我几乎无法理解为什么会这样。有什么想法吗?

1 个答案:

答案 0 :(得分:10)

Html.*帮助程序(例如Html.TextBoxForHtml.CheckBoxFor,...仅在表单内使用时才会发出验证属性。因此,请确保将其包裹在Html.BeginForm电话中。就客户端验证而言,您应该在更新DOM之后调用jQuery.validator.unobtrusive.parse方法以重新应用客户端验证。并yet another article

如果您没有表格,可以作弊并将以下内容放入部分:

@model MyViewModel
@{
    ViewContext.FormContext = new FormContext();
}
@Html.EditorFor(x => x.Foo)

现在帮助器将在输入字段上发出data- * validation属性。