类属性不适用于EditorFor / TextBoxFor

时间:2014-05-29 20:56:37

标签: jquery asp.net-mvc asp.net-mvc-3

我试图在一个表上应用js插件,其中行由我的模型对象上的List属性填充。但是,@class属性都没有。

的jQuery

$(document).ready(function ($) {
    //These all work
    $('#HomePhone').mask("(999) 999-9999? x99999");
    $('#BusinessPhone').mask("(999) 999-9999? x99999");
    $('#MobilePhone').mask("(999) 999-9999");
    $('#FaxNumber').mask("(999) 999-9999? x99999");
    $('#Birthdate').mask("99/99/9999");
    $('#HomeZip').mask("99999?-9999");
    $('#MailingZip').mask("99999?-9999");
    $('#LocationZip').mask("99999?-9999");
    $('#LifeEAPercent').mask("9.99");
    $('#VARPercent').mask("9.99");

    //These don't
    $("#relCodeEffDate").datepicker({
        dateFormat: 'mm/dd/yy'
    });
    $("#relDistCode").numeric();
});

剃刀

@for (int i = 0; i < Model.AgentRelationshipCodes.Count(); i++)
{
    <tr>
        <td>
            @Html.TextBoxFor(model => model.AgentRelationshipCodes[i].RelationshipId, new {@class="relDistCode", maxlength = 3})
        </td>
        <td>
            @Html.EditorFor(model => model.AgentRelationshipCodes[i].EffectiveDate, "NullableDate", new {@class = "relCodeEffDate"})
        </td>
        @Html.HiddenFor(model => model.AgentRelationshipCodes[i].ID)
        @Html.HiddenFor(model => model.AgentRelationshipCodes[i].ID)
    </tr>
}

呈现HTML

<tr>
    <td>
        <input class="relDistCode" id="AgentRelationshipCodes_0__RelationshipId" maxlength="3" name="AgentRelationshipCodes[0].RelationshipId" type="text" value="" />
    </td>
    <td>                                                       
        <input class="form-control" id="AgentRelationshipCodes_0__EffectiveDate" name="AgentRelationshipCodes[0].EffectiveDate" type="text" value="" />
    </td>
    <input data-val="true" data-val-number="The field ID must be a number." data-val-required="The ID field is required." id="AgentRelationshipCodes_0__ID" name="AgentRelationshipCodes[0].ID" type="hidden" value="0" />
    <input id="AgentRelationshipCodes_0__ID" name="AgentRelationshipCodes[0].ID" type="hidden" value="0" />
</tr>
<tr>
    <td>
        <input class="relDistCode" id="AgentRelationshipCodes_1__RelationshipId" maxlength="3" name="AgentRelationshipCodes[1].RelationshipId" type="text" value="" />
    </td>
    <td>                                                       
        <input class="form-control" id="AgentRelationshipCodes_1__EffectiveDate" name="AgentRelationshipCodes[1].EffectiveDate" type="text" value="" />
    </td>
    <input data-val="true" data-val-number="The field ID must be a number." data-val-required="The ID field is required." id="AgentRelationshipCodes_1__ID" name="AgentRelationshipCodes[1].ID" type="hidden" value="0" />
    <input id="AgentRelationshipCodes_1__ID" name="AgentRelationshipCodes[1].ID" type="hidden" value="0" />
</tr>
<tr>
    <td>
        <input class="relDistCode" id="AgentRelationshipCodes_2__RelationshipId" maxlength="3" name="AgentRelationshipCodes[2].RelationshipId" type="text" value="" />
    </td>
    <td>                                                       
        <input class="form-control" id="AgentRelationshipCodes_2__EffectiveDate" name="AgentRelationshipCodes[2].EffectiveDate" type="text" value="" />
    </td>
    <input data-val="true" data-val-number="The field ID must be a number." data-val-required="The ID field is required." id="AgentRelationshipCodes_2__ID" name="AgentRelationshipCodes[2].ID" type="hidden" value="0" />
    <input id="AgentRelationshipCodes_2__ID" name="AgentRelationshipCodes[2].ID" type="hidden" value="0" />
</tr>
<tr>
    <td>
        <input class="relDistCode" id="AgentRelationshipCodes_3__RelationshipId" maxlength="3" name="AgentRelationshipCodes[3].RelationshipId" type="text" value="" />
    </td>
    <td>                                                       
        <input class="form-control" id="AgentRelationshipCodes_3__EffectiveDate" name="AgentRelationshipCodes[3].EffectiveDate" type="text" value="" />
    </td>
    <input data-val="true" data-val-number="The field ID must be a number." data-val-required="The ID field is required." id="AgentRelationshipCodes_3__ID" name="AgentRelationshipCodes[3].ID" type="hidden" value="0" />
    <input id="AgentRelationshipCodes_3__ID" name="AgentRelationshipCodes[3].ID" type="hidden" value="0" />
</tr>
<tr>
    <td>
        <input class="relDistCode" id="AgentRelationshipCodes_4__RelationshipId" maxlength="3" name="AgentRelationshipCodes[4].RelationshipId" type="text" value="" />
    </td>
    <td>                                                       
        <input class="form-control" id="AgentRelationshipCodes_4__EffectiveDate" name="AgentRelationshipCodes[4].EffectiveDate" type="text" value="" />
    </td>
    <input data-val="true" data-val-number="The field ID must be a number." data-val-required="The ID field is required." id="AgentRelationshipCodes_4__ID" name="AgentRelationshipCodes[4].ID" type="hidden" value="0" />
    <input id="AgentRelationshipCodes_4__ID" name="AgentRelationshipCodes[4].ID" type="hidden" value="0" />
</tr>

2 个答案:

答案 0 :(得分:2)

他们是Class,而不是id。使用期间而不是哈希:

    $(".relCodeEffDate").datepicker({
        dateFormat: 'mm/dd/yy'
    });
    $(".relDistCode").numeric();

答案 1 :(得分:0)

我在下面的答案中完全错过了class / id hash / period,但是这个问题的第二层是我正在使用的NullableDate助手。这被设置为给帮助者一类form-control。我需要在部分视图中更改类,以便datepicker能够工作。

@model DateTime?

@Html.TextBox("", (Model.HasValue ? Model.Value.ToString("MM/dd/yyyy") : string.Empty), new { @class = "relCodeEffDate" })