【问题标题】:DateTime Field in ASP.Net Framework MVC Entity 6ASP.Net 框架 MVC 实体 6 中的日期时间字段
【发布时间】:2020-08-27 05:03:14
【问题描述】:

类似于Datetime field in MVC Entity Framework,但不完全是。他们的解决方案也没有解决我的问题。我有一个带有种子数据的 dB 和以下内容,

具有可为空的 DateTime 属性的模型:

[DataType(DataType.Time)]
[DisplayFormat(DataFormatString = "{0:hh:mm tt}", ApplyFormatInEditMode = true)]
[Display(Name = "Evening Showtime")]
public DateTime? ShowtimeEve { get; set; }

名为 Productions.cshtml 的 Razor 页面视图:

@model TheatreCMS.Models.Production
@using TheatreCMS.Controllers
@{
  ViewBag.Title = "Edit";
}

@Styles.Render("~/Content/Site.css")

<h2>Edit</h2>

@using (Html.BeginForm("Edit", "Productions", null, FormMethod.Post, new { enctype = "multipart/form-data" }))
{
  @Html.AntiForgeryToken()
  <div class="formContainer2">
    <div class="form-horizontal">
      <h4>Production</h4>
      <hr />
      @Html.ValidationSummary(true, "", new { @class = "text-danger" })
      @Html.HiddenFor(model => model.ProductionId)
      
      <div class="form-group">
        @Html.LabelFor(model => model.ShowtimeEve, htmlAttributes: new { @class = "control-label col-md-2 inputLabel" })
        <div class="col-md-10 formBox">
          @Html.EditorFor(model => model.ShowtimeEve, new { htmlAttributes = new { @class = "form-control" } })
          @Html.ValidationMessageFor(model => model.ShowtimeEve, "", new { @class = "text-danger" })
        </div>
      </div>
}

还有一个控制器 ProductionsController.cs 使用 DBContext 的继承将数据传递给视图(这是一个代码优先项目):

[HttpGet]
public ActionResult Edit(int? id)
{
   if (id == null)
  {
      return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
   }
   Production production = db.Productions.Find(id);
   if (production == null)
   {
      return HttpNotFound();
    }

   return View(production);
}

视图上的表单不会填充创建的输入字段中的数据。它正在创建一个空白的时间选择器字段。我需要它是一个数据填充时间选择器字段。这是一个编辑视图,因此目标是让所有字段都填充数据,向客户展示他们将要更改的内容。

我一步一步地运行调试器,检查输出值。它正在将 DateTime 值从 Controller 传递给 View,但之后它在某处丢失了。

这是渲染的样子:

【问题讨论】:

  • 渲染出来的 HTML 是什么样子的
  • 我认为这是因为您声明的变量是Datetime,而您强制指定的类型是Datatype.time
  • @AbdulHaseeb 如果我没记错的话,DataType.Time 就像DataType.Date 一样,是从基础DataType.Datetime 继承而来的。至少对于[DataAnnotations],就是这样。我知道在 C# 中没有“时间”数据类型,至少本机没有。那只是 DateTime,然后用 DateTime.ToString("hh:mm tt") 格式化。
  • @Vince 原始 html 呈现为&lt;input class="form-control text-box single-line" id="ShowtimeEve" name="ShowtimeEve" type="time" value="05:30 PM"&gt;,但在将[DataAnnotation] 中的{0:hh:mm tt} 更改为{0:hh:mm} 后,它以24 小时发送。格式化的时间,这是它所需要的,因此可以正确呈现&lt;input class="form-control text-box single-line" id="ShowtimeEve" name="ShowtimeEve" type="time" value="05:30"&gt;

标签: c# asp.net-mvc model-view-controller entity-framework-6 datetime-format


【解决方案1】:

我会给你一些建议,问题应该是其中之一。

像这样更改日期时间格式DataFormatString = "{0:hh:mm}"

当我查看您的 get 操作时,我注意到您正在使用实体,但是 您的视图页面模型是模型。您应该将实体数据转换为 您的模型并返回您的视图页面模型。但是你的模型和 实体是同一个类,这不应该是问题。例如你的行为;

[HttpGet]
    public ActionResult Edit(int? id)
    {
       if (id == null)
       {
          return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
       }
       Production production = db.Productions.Find(id);
       if (production == null)
       {
          return HttpNotFound();
       }
       else
       {
          var productionModel = new Production();//TheatreCMS.Models.Production
          productionModel.ShowtimeEve = production.ShowtimeEve;
          //other converts here

       }
    
       return View(productionModel);
    }

我的其他建议,尝试在视图中使用此代码;

@Html.TextBoxFor(m => m.ShowtimeEve, new { @class = "form-control" })

【讨论】:

  • 感谢您深入了解如何更好地使用实体框架。不幸的是,我无法控制程序的结构。我只是个新手,正在处理自动取款机中的错误。
  • 没问题,只需设置一个警报您的视图并使用此脚本查看 model.ShowtimeEve; $(document).ready(function () { alert(@Model.ShowtimeEve)}
【解决方案2】:

我找到了答案。我把问题复杂化了。虽然每个人都提供了很好的答案和见解,但真正的问题是 HTML5 问题以及 value 属性如何发送到视图。表单输入标签上的 value 属性的格式必须是 24 小时制。格式,而不是 12 小时。

所以只要改变:

[DisplayFormat(DataFormatString = "{0:hh:mm tt}", ApplyFormatInEditMode = true)]

[DisplayFormat(DataFormatString = "{0:HH:mm}", ApplyFormatInEditMode = true)]

发送到制作的编辑视图的输入元素的值现在在 24 小时内呈现。格式允许时间选择器正确地重新格式化回 12 小时。查看。

它一直在检查员那里!

感谢大家的帮助。我是 C# 新手,更不用说 ASP.NET Entity MVC,因此非常感谢支持。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-25
    • 1970-01-01
    • 1970-01-01
    • 2011-06-06
    • 1970-01-01
    相关资源
    最近更新 更多