【问题标题】:Display Current DateTime in ASP.NET MVC 5在 ASP.NET MVC 5 中显示当前日期时间
【发布时间】: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


【解决方案1】:
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

【讨论】:

  • 它不能工作,它显示“订单”不包含“订单元数据”的定义,并且找不到接受“订单”类型的第一个参数的可访问扩展方法“订单元数据”(是您缺少 using 指令或程序集引用?)
【解决方案2】:

您的错误在于您的操作方法不返回模型实例,因此模型为空,您在文本框中看不到任何内容。

像这样改变你的行为:

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

并从这里更改视图中的模型:

@model Food_Founder.Models.Order

到这里:

@model Food_Founder.Models.OrderMetaData

然后更改“发布方法”以获取 OrderMetaData 模型,如下所示:

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

然后它就可以正常工作了。

希望对你有所帮助。

【讨论】:

  • 它不能工作。它显示Argument 1: cannot convert from 'Food_Founder.Models.OrderMetaData' to 'Food_Founder.Models.Order' Food_Founder
【解决方案3】:

调试代码并检查值 orderDetail.OrderDate = System.DateTime.Now; 是否为空或当前日期分配正确?

【讨论】:

  • 当前日期分配正确,但未显示在查看页面上。
  • 虽然是有用的信息,但我认为这更适合放在评论部分,就在问题下方,仅仅是因为它提供了建议,而不是问题的实际解决方案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多