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

tug*_*erk 6 asp.net-mvc asp.net-mvc-templates asp.net-mvc-validation asp.net-mvc-3

我的一个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>
Run Code Online (Sandbox Code Playgroud)

这是我应该得到的:

<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>
Run Code Online (Sandbox Code Playgroud)

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");
    }
Run Code Online (Sandbox Code Playgroud)

我几乎无法理解为什么会这样.任何的想法?

Dar*_*rov 10

Html.*诸如佣工Html.TextBoxFor,Html.CheckBoxFor...发出一个表单中使用验证仅属性.因此,请确保将它们包裹在一个Html.BeginForm电话中.就客户端验证而言,您应该jQuery.validator.unobtrusive.parse在更新DOM之后调用该方法以重新应用客户端验证.而另一篇文章.

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

@model MyViewModel
@{
    ViewContext.FormContext = new FormContext();
}
@Html.EditorFor(x => x.Foo)
Run Code Online (Sandbox Code Playgroud)

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

  • @tugberk,这告诉助手他们在一个表单中:-)基本上在所有html帮助器中都有一个测试,当前的ViewContext.FormContext属性是否为null.并且只有在它不为空时才生成data-*属性.当您使用表单时,它会自动实例化,但是当您没有表单时,它将为null. (2认同)