[英]Display Current DateTime in 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")
}
因為我一直使用樣式表,所以沒有使用引導程序,也沒有用樣式表修改視圖頁面。 但是,我的輸出仍未在文本框中顯示當前日期。 我在此帖子上看到並遵循了這個問題, 當前日期和時間-MVC剃須刀中的默認值 。 我犯的錯誤在哪里?
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
您的錯誤在於您的操作方法中,該方法不返回模型實例,因此模型為null,並且您在文本框中看不到任何內容。
像這樣改變你的動作:
[Authorize]
[HttpGet]
public ActionResult PlaceOrder()
{
var model=new OrderMetaData();
return View(model);
}
並據此更改模型:
@model Food_Founder.Models.Order
對此:
@model Food_Founder.Models.OrderMetaData
然后更改“ post方法”以獲取如下的OrderMetaData模型:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult PlaceOrder(OrderMetaData orderDetail)
{
然后它可以正常工作。
希望對您有所幫助。
調試代碼並檢查值orderDetail.OrderDate = System.DateTime.Now; 是否為null或當前日期分配正確?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.