Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将日期时间从视图捕获到数据库MVC中_C#_Asp.net Mvc 4 - Fatal编程技术网

C# 将日期时间从视图捕获到数据库MVC中

C# 将日期时间从视图捕获到数据库MVC中,c#,asp.net-mvc-4,C#,Asp.net Mvc 4,我创建了一个MVC应用程序,它在数据库中发布了一个文本框。发布文本框时,我希望从视图中捕获今天的日期并将其存储到表中。如何捕获发布的视图的日期和时间,以及如何将此信息传递到数据库中的行日期?谢谢你的帮助 控制器 [HttpGet] public ActionResult Pay(int accountNumber) { return View(); } [HttpPost] public ActionResult Pay(Payme

我创建了一个MVC应用程序,它在数据库中发布了一个文本框。发布文本框时,我希望从视图中捕获今天的日期并将其存储到表中。如何捕获发布的视图的日期和时间,以及如何将此信息传递到数据库中的行日期?谢谢你的帮助

控制器

    [HttpGet]
    public ActionResult Pay(int accountNumber)
    {
        return View();
    }
    [HttpPost]
    public ActionResult Pay(Payments payment )
    {
        if(ModelState.IsValid)
        {
            DB.Payment.Add(payment);
            DB.SaveChanges();
            var service = new Accounts(DB);
            service.Updatepayee(payment.AccountNumber);
            return RedirectToAction("Index", "Home");
        }
        return View();

    }
付款类别

public class Payments

   public int Id { get; set; }

        [Required]
        [DataType(DataType.Currency)]
        [DisplayFormat(ConvertEmptyStringToNull = false)]
        public decimal Amount { get; set; }

        [Required]
        public int AccountNumber {get; set;}


        [RegularExpression(@"(^$)|(^\d{2}/\d{2}/\d{4})|(^((\d{1})|(\d{2}))/((\d{1})|(\d{2}))/(\d{4})\s((\d{1})|(\d{2}))[:]{1}((\d{1})|(\d{2}))[:]{1}((\d{1})|(\d{2}))\s((AM)|(PM)))", ErrorMessage = "Invalid Date")]
        public DateTime TransactionDate { get; set; }
        //Navigation property to check the account 

 public virtual AccountNumber accountNumber { get; set; }
    }
付款方式

 @model Credits.View.Payments

 @{
     ViewBag.Title = "Pay"; }

 <h2>Payments</h2>


 @using (Html.BeginForm())  {
     @Html.AntiForgeryToken()

     <div class="form-horizontal">
         <h4>Transaction</h4>
         <hr />
         @Html.ValidationSummary(true, "", new { @class = "text-danger" })
         <div class="form-group">
             @Html.LabelFor(model => model.Amount, htmlAttributes: new { @class = "control-label col-md-2" })
             <div class="col-md-10">
                 @Html.EditorFor(model => model.Amount, new { htmlAttributes = new { @class = "form-control" } })
                 @Html.ValidationMessageFor(model => model.Amount, "", new { @class = "text-danger" })
           </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", "Home") </div>

 @section Scripts {
     @Scripts.Render("~/bundles/jqueryval") }
@model Credits.View.Payments
@{
ViewBag.Title=“支付”}
付款
@使用(Html.BeginForm()){
@Html.AntiForgeryToken()
交易

@Html.ValidationSummary(true,“,new{@class=“text danger”}) @LabelFor(model=>model.Amount,htmlAttributes:new{@class=“controllabel col-md-2”}) @EditorFor(model=>model.Amount,new{htmlAttributes=new{@class=“form control”}) @Html.ValidationMessageFor(model=>model.Amount,“,new{@class=“text danger”}) } @ActionLink(“返回列表”、“索引”、“主页”) @节脚本{ @Scripts.Render(“~/bundles/jqueryval”)}
您无法在控制器中设置它:

[HttpPost]
public ActionResult Pay(Payments payment )
{
    if(ModelState.IsValid)
    {
        payment.TransactionDate = DateTime.Now.ToUniversalTime();
        DB.Payment.Add(payment);
        DB.SaveChanges();

无法在控制器中设置它:

[HttpPost]
public ActionResult Pay(Payments payment )
{
    if(ModelState.IsValid)
    {
        payment.TransactionDate = DateTime.Now.ToUniversalTime();
        DB.Payment.Add(payment);
        DB.SaveChanges();

您的
TransactionDate
具有绑定到特定区域性和格式的正则表达式验证器。这是故意的吗?您是否准备将其他有效的24小时日期/时间格式视为无效而拒绝?它在删除正则表达式后起作用,并感谢您的回答您的
TransactionDate
具有绑定到特定区域性和格式的正则表达式验证程序。这是故意的吗?您是否准备将其他有效的24小时日期/时间格式视为无效而拒绝?它在删除正则表达式后起作用,并感谢您添加了应答日期,但事务不起作用??我在文本框中发布的号码没有将数据发送到表中?@Dai可能在轨道上。日期值不带格式。您的正则表达式可能可以应用于文本框,但不能应用于属性本身。请如何更改?首先,请尝试删除正则表达式属性。Gustav非常感谢您在删除正则表达式后它起了很大的作用:)添加了日期,但事务不起作用??我在文本框中发布的号码没有将数据发送到表中?@Dai可能在轨道上。日期值不带格式。您的正则表达式可能可以应用于文本框,但不能应用于属性本身。请如何更改?首先,请尝试删除正则表达式属性。Gustav非常感谢您在删除正则表达式后,它起了很大的作用:)