【问题标题】:data passed from view does not remain after post back从视图传递的数据在回发后不会保留
【发布时间】:2019-07-04 16:14:30
【问题描述】:

我无法找到解决方案,因为我认为这是一个错误,或者我没有看到我应该拥有的东西。

我将模型从受控传递到作为强类型数据的视图。但是,只有一个参数有问题,它在回发后清除它的数据。 当我点击搜索.. 您可以在这里看到,Closed Time 的日期仍然存在,但 Cut Off 时间的文本已经消失。最后看到的日期是@Model.CutOffTimeFrom - @Model.CutOffTimeTo的值,只是为了看数据是被清除还是删除,其实不是,只是EditorFor上的显示被删除了。

我也试过这个one,使用<input>标签,但它仍然是相同的输出。

下面是我的模型:

[AssertThat("CutOffTimeFrom <= CutOffTimeTo", ErrorMessage = "Date To should be greater than Date From")]
    [RequiredIf("CutOffTimeFrom != null", ErrorMessage = "Date From is required")]
    [DataType(DataType.Date)]
    [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
    public DateTime? CutOffTimeFrom { get; set; }

    [RequiredIf("CutOffTimeTo != null", ErrorMessage = "Cut Off Time From is required")]
    [DataType(DataType.Date)]
    [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
    public DateTime? CutOffTimeTo { get; set; }

这是视图:

<div>
     @*<input type="date" name="CutOffTimeFrom" value="@Model.CutOffTimeFrom" class="form-control input-sm" />-<input type="date" name="CutOffTimeTo" value="@Model.CutOffTimeTo" class="form-control input-sm" />*@
     @Html.EditorFor(m => m.CutOffTimeFrom, new { htmlAttributes = new { @class = "form-control input-sm" } }) - @Html.EditorFor(m => m.CutOffTimeTo, new { htmlAttributes = new { @class = "form-control input-sm" } })
     @Html.ValidationMessageFor(model => model.CutOffTimeFrom, "", new { @class = "text-danger" })
     @Html.ValidationMessageFor(model => model.CutOffTimeTo, "", new { @class = "text-danger" })
</div>

所有其他字段都可以正常工作。只有Cut Off时间被清除,虽然它满足搜索条件,但仍然在模型上传递值,但它只是不显示在视图上。

有人遇到过这个问题吗?

【问题讨论】:

    标签: html razor model-view-controller model postback


    【解决方案1】:

    我们可以看到控制器处理这个吗,因为它可能不是模型绑定

    【讨论】:

      【解决方案2】:

      我彻底检查了每个元素、属性、参数的差异,并看到了一个差异,即日期格式。在检查每个EditorFor 的inspect 元素时发现了这个错误,发现它们是不同的日期,2019/02/0802/08/2019,后者是错误的。

      更改自:

      [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
      

      收件人:

      [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
      

      [DataType(DataType.Date)] 可能无法呈现 2019 年 2 月 8 日,这就是它没有重新填充表单的原因。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-04-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多