代码之家  ›  专栏  ›  技术社区  ›  AdorableVB

从视图传递的数据在回发后不保留

  •  0
  • AdorableVB  · 技术社区  · 6 年前

    我找不到解决方法,因为我认为这是一个bug,或者我可能没有看到我应该拥有的东西。

    我正在将一个模型作为强类型数据从Controlled传递到View。但是,只有一个参数被窃听,它会在回发后清除它的数据。 enter image description here 当我单击搜索时… enter image description here 您可以在这里看到,从关闭时间开始的日期仍然存在,但从关闭时间开始的文本已经不存在了。你在结尾看到的日期是 @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>
    

    所有其他领域的工作都很好。只有截止时间被清除,虽然它满足搜索条件,但值仍然在模型上传递,但它只是不显示在视图上。

    有人遇到这个问题吗?

    2 回复  |  直到 6 年前
        1
  •  0
  •   Makhele Sabata    6 年前

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

        2
  •  0
  •   AdorableVB    6 年前

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

    更改自:

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

    到:

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

    大概是 [DataType(DataType.Date)] 无法提交2019年8月2日,这就是它不重新填充表单的原因。