【发布时间】:2020-01-12 11:41:55
【问题描述】:
我想将当前时间显示为数据库中表的数据。在代码下面我已经为它写了一些代码,但是日期的文本框仍然是空的。
这是我的课
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。我的错误在哪里?
【问题讨论】:
-
您的 CurrentDate 属性可以为空,所以我相信您必须这样做:get { if (CurrentDate.HasValue){ return CurrentDate; } else { 返回日期时间。今天; }
-
@user10728126 不......仍然无法正常工作。文本框仍为空白。
-
你能发表你的看法吗?
-
@user10728126 是的,我只是用代码和图像发布它
-
您的绑定模型显示 @model Food_Founder.Models.Order 但您发布的模型标题为 OrderMetaData。如果您希望 OrderMetaData 类绑定到您的视图,则必须在名称空间相同的情况下将视图中的模型更改为:“@model Food_Founder.Models.OrderMetaData”
标签: c# asp.net-mvc