c# - 在 ASP.NET MVC 5 中显示当前日期时间
问题描述
我想将当前时间显示为数据库中表的数据。在代码下方,我为它编写了一些代码,但日期的文本框仍然为空。
这是我的课
public class OrderMetaData
{
public string OrderAddress { get; set; }
public int OrderPrice { get; set; }
[DataType(DataType.Date)]
private DateTime? CurrentDate;
[Display(Name = "Order Date:")]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")]
public Nullable<System.DateTime> OrderDate
{
get { return CurrentDate ?? DateTime.Today; }
set { CurrentDate = value; }
}
[Display(Name = "Amount of Chicken Chop with Black Pepper Sauce")]
public int A_ChickenChop_BP { get; set; }
[Display(Name = "Amount of Chicken Chop with Mushroom Sauce")]
public int A_ChickenChop_M { get; set; }
[Display(Name = "Amount of Spaghetti in Angel Hair")]
public int A_Spaghetti_AH { get; set; }
[Display(Name = "Amount of Spaghetti in Penne")]
public int A_Spaghetti_P { get; set; }
[Display(Name = "Amount of Spaghetti in Shells")]
public int A_Spaghetti_S { get; set; }
[Display(Name = "Amount of Chicken Rice with chicken breast part")]
public int A_ChickenRice_CB { get; set; }
[Display(Name = "Amount of Chicken Rice with chicken wing part")]
public int A_ChickenRice_CW { get; set; }
[Display(Name = "Amount of Chicken Rice with drumstick part")]
public int A_ChickenRice_D { get; set; }
[Display(Name = "Amount of Non-Spicy Wantan Mee")]
public int A_WantanMee_NS { get; set; }
[Display(Name = "Amount of Spicy Wantan Mee")]
public int A_WantanMee_IS { get; set; }
}
这是我的控制器
[Authorize]
[HttpGet]
public ActionResult PlaceOrder()
{
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult PlaceOrder(Order orderDetail)
{
String message = "";
using (myDatabaseEntities1 myDatabase1 = new myDatabaseEntities1())
{
orderDetail.OrderDate = System.DateTime.Now;
//WF
Double PriceOfF1 = Convert.ToDouble(orderDetail.A_ChickenChop_BP.GetValueOrDefault()) * 14.9;
Double PriceOfF2 = Convert.ToDouble(orderDetail.A_ChickenChop_M.GetValueOrDefault()) * 14.9;
Double PriceOfF3 = Convert.ToDouble(orderDetail.A_Spaghetti_AH.GetValueOrDefault()) * 10.9;
Double PriceOfF4 = Convert.ToDouble(orderDetail.A_Spaghetti_P.GetValueOrDefault()) * 10.9;
Double PriceOfF5 = Convert.ToDouble(orderDetail.A_Spaghetti_S.GetValueOrDefault()) * 10.9;
//CF
Double PriceOfF6 = Convert.ToDouble(orderDetail.A_ChickenRice_CB.GetValueOrDefault()) * 6.9;
Double PriceOfF7 = Convert.ToDouble(orderDetail.A_ChickenRice_CW.GetValueOrDefault()) * 6.9;
Double PriceOfF8 = Convert.ToDouble(orderDetail.A_ChickenRice_D.GetValueOrDefault()) * 6.9;
Double PriceOfF9 = Convert.ToDouble(orderDetail.A_WantanMee_NS.GetValueOrDefault()) * 6.9;
Double PriceOfF10 = Convert.ToDouble(orderDetail.A_WantanMee_IS.GetValueOrDefault()) * 6.9;
Double T_Price = orderDetail.OrderPrice;
T_Price = PriceOfF1 + PriceOfF2 + PriceOfF3 + PriceOfF4 + PriceOfF5 +
PriceOfF6 + PriceOfF7 + PriceOfF8 + PriceOfF9 + PriceOfF10;
if (T_Price > 1)
{
myDatabase1.Orders.Add(orderDetail);
myDatabase1.SaveChanges();
message = "The order has been placed";
orderDetail.IsPlaced = true;
}
else
{
message = "Please select at least one of the food";
orderDetail.IsPlaced = false;
}
}
ViewBag.Message = message;
return View(orderDetail);
}
我已经将控制器中的代码编写为orderDetail.OrderDate = System.DateTime.Now;
,在类中编写为
[DataType(DataType.Date)]
private DateTime? CurrentDate;
[Display(Name = "Order Date:")]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")]
public Nullable<System.DateTime> OrderDate
{
get { return CurrentDate ?? DateTime.Today; }
set { CurrentDate = value; }
}
代码下面是我的视图代码
@model Food_Founder.Models.Order
@{
ViewBag.Title = "PlaceOrder";
}
<h2>PlaceOrder</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@ViewBag.Message
<div class="form-horizontal">
<h4>Order</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.User_ID, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.User_ID, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.User_ID, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.OrderDate, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.OrderDate, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.OrderDate, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.OrderAddress, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.OrderAddress, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.OrderAddress, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.OrderPrice, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.OrderPrice, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.OrderPrice, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.A_ChickenChop_BP, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.A_ChickenChop_BP, new { htmlAttributes = new { @class = "form-control", @Value = "0" } })
@Html.ValidationMessageFor(model => model.A_ChickenChop_BP, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.A_ChickenChop_M, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.A_ChickenChop_M, new { htmlAttributes = new { @class = "form-control", @Value = "0" } })
@Html.ValidationMessageFor(model => model.A_ChickenChop_M, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.A_Spaghetti_AH, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.A_Spaghetti_AH, new { htmlAttributes = new { @class = "form-control", @Value = "0" } })
@Html.ValidationMessageFor(model => model.A_Spaghetti_AH, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.A_Spaghetti_P, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.A_Spaghetti_P, new { htmlAttributes = new { @class = "form-control", @Value = "0" } })
@Html.ValidationMessageFor(model => model.A_Spaghetti_P, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.A_Spaghetti_S, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.A_Spaghetti_S, new { htmlAttributes = new { @class = "form-control", @Value = "0" } })
@Html.ValidationMessageFor(model => model.A_Spaghetti_S, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.A_ChickenRice_CB, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.A_ChickenRice_CB, new { htmlAttributes = new { @class = "form-control", @Value = "0" } })
@Html.ValidationMessageFor(model => model.A_ChickenRice_CB, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.A_ChickenRice_CW, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.A_ChickenRice_CW, new { htmlAttributes = new { @class = "form-control", @Value = "0" } })
@Html.ValidationMessageFor(model => model.A_ChickenRice_CW, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.A_ChickenRice_D, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.A_ChickenRice_D, new { htmlAttributes = new { @class = "form-control", @Value = "0" } })
@Html.ValidationMessageFor(model => model.A_ChickenRice_D, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.A_WantanMee_NS, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.A_WantanMee_NS, new { htmlAttributes = new { @class = "form-control", @Value = "0" } })
@Html.ValidationMessageFor(model => model.A_WantanMee_NS, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.A_WantanMee_IS, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.A_WantanMee_IS, new { htmlAttributes = new { @class = "form-control", @Value = "0" } })
@Html.ValidationMessageFor(model => model.A_WantanMee_IS, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.IsPlaced, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
<div class="checkbox">
@Html.EditorFor(model => model.IsPlaced)
@Html.ValidationMessageFor(model => model.IsPlaced, "", new { @class = "text-danger" })
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
因为我总是使用我的样式表,所以我没有使用引导程序,也没有使用我的样式表修改视图页面。但是,我的输出仍然没有在文本框中显示当前日期。我在这篇文章中看到了这个问题并关注它Current date and time - Default in MVC razor。我犯的错误在哪里?
解决方案
In your OrderMetaData Class You can use as,
`[DataType(DataType.Date)]
[Display(Name = "Order Date")]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")]
private DateTime OrderDate { get; set; } `
并在视图类
`@Html.DisplayNameFor(model => model.OrderMetaData.OrderDate )`
It worked for me
推荐阅读
- c# - 如何将 WiX 的“产品版本”自动设置为 Asp.NET Core 的“包版本”?
- excel - 嵌套if计算在excel中
- javascript - 在 WordPress 中使用 javascript 的 iframe 动态 url
- uwp - UWP 发布构建失败
- php - 更改字符串php中括号内的所有文本颜色
- delphi - 在 Delphi / C++ Builder 中使用 WebView (EdgeHTML)
- c# - 如何在 void 中返回变量
- javascript - 如何使用 Jest 将 prop 中的 spyOn 方法传递给组件?
- nginx - Tengine Web 服务器默认安装支持哪些脚本语言?
- docker - 通过 docker 下载并运行带有 gnome 支持的 ubuntu-desktop