模型回发与html属性

时间:2018-04-25 19:33:34

标签: asp.net-mvc

我遇到了使用JQuery datepicker在回发后在我的ViewModel中获取值的问题。回发具有正确的日期,表单组StartDate没有附加到EditorFor的新htmlAttribute。由于htmlAttribute,StopDate不回发所选日期。我宁愿不使用隐藏字段来发回数据,因为我知道这应该有效。有人有什么建议吗?

    <div class="form-group" id="StartDate">
        @Html.LabelFor(model => model.StartDate, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.reportTimeEntries[0].StartDate, "","StartDate" )
            @Html.ValidationMessageFor(model => model.StartDate, "", new { @class = "text-danger" })
            @Html.HiddenFor(model => model.StartDate)
        </div>
    </div>

            <div class="form-group" id="StopDate">
        @Html.LabelFor(model => model.StopDate, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.reportTimeEntries[0].StopDate, new { htmlAttributes = new { @class = "form-control date" } })
            @Html.ValidationMessageFor(model => model.StopDate, "", new { @class = "text-danger" })
        </div>
    </div>

    $('#StartDate .date').datepicker({
        'format': 'mm/dd/yyyy',
        'autoclose': true
    });

    $('#StopDate .date').datepicker({
        'format': 'mm/dd/yyyy',
        'autoclose': true
    });

更新
所以看起来这可以通过将其添加到我的ViewModel来实现。 'Nullable'部分似乎允许回发日期。
视图模型

[DataType(DataType.DateTime)]
[Display(Name = "Stop Date")]
[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}", ApplyFormatInEditMode = true)]
public Nullable<System.DateTime> StopDate { get; set; }

查看

<div class="form-group" id="StopDate">
    @Html.LabelFor(model => model.StopDate, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.EditorFor(model => model.reportTimeEntries[0].StopDate, "","StopDate",new { htmlAttributes = new { @class = "form-control date" } })
        @Html.ValidationMessageFor(model => model.StopDate, "", new { @class = "text-danger" })
    </div>
</div>

0 个答案:

没有答案