简体   繁体   English

在ASP.NET MVC 5中显示当前日期时间

[英]Display Current DateTime in ASP.NET MVC 5

I want display the current time as the data for the table in database. 我想将当前时间显示为数据库中表的数据。 Below the code I have write some code for it but the text box for date still empty. 在代码下方,我为此编写了一些代码,但日期文本框仍然为空。

This is my class 这是我的课

    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; }
    }

And this is my controller 这是我的控制器

        [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);
        }

I have write the code in the controller as orderDetail.OrderDate = System.DateTime.Now; 我已经在控制器中将代码编写为orderDetail.OrderDate = System.DateTime.Now; and inside the class as 在班级里

        [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; }
        }

Below the code is my view code 代码下面是我的查看代码

@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")
}

And the images of that view is here 这个视图的图像在这里 在此处输入图片说明

As I always use my style sheet so I didn't use the bootstrap and I haven't modify the view page with my style sheet. 因为我一直使用样式表,所以没有使用引导程序,也没有用样式表修改视图页面。 However, my output still didn't show the current date inside the text box. 但是,我的输出仍未在文本框中显示当前日期。 This problem I have see on this post and follow it Current date and time - Default in MVC razor . 我在此帖子上看到并遵循了这个问题, 当前日期和时间-MVC剃须刀中的默认值 Where is the mistake I make? 我犯的错误在哪里?

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; } `

and in view class 并在课堂上

 `@Html.DisplayNameFor(model => model.OrderMetaData.OrderDate )`

It worked for me

your mistake is in your action Method that don't return instance of model so model is null and you can't see anything in textbox. 您的错误在于您的操作方法中,该方法不返回模型实例,因此模型为null,并且您在文本框中看不到任何内容。

change your action like this : 像这样改变你的动作:

[Authorize]
[HttpGet]
public ActionResult PlaceOrder()
{
    var model=new OrderMetaData();
    return View(model);
}

and also change model in view from this : 并据此更改模型:

@model Food_Founder.Models.Order

to this : 对此:

@model Food_Founder.Models.OrderMetaData

then change "post method" to get OrderMetaData model like this : 然后更改“ post方法”以获取如下的OrderMetaData模型:

[HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult PlaceOrder(OrderMetaData orderDetail)
        {

and then it work properly. 然后它可以正常工作。

I hope it been helpful . 希望对您有所帮助。

Debug the code and check the value orderDetail.OrderDate = System.DateTime.Now; 调试代码并检查值orderDetail.OrderDate = System.DateTime.Now; is null or current date assigning correctly? 是否为null或当前日期分配正确?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM