2016-12-03 1 views
0

Ich habe MVC-Anwendung erstellt, die ein Textfeld in der Datenbank veröffentlicht hat. Wenn das Textfeld veröffentlicht wird, möchte ich das aktuelle Datum aus der Ansicht erfassen und in der Tabelle speichern. Wie erfasse ich Datum und Uhrzeit der übermittelten Ansicht und wie übermittele ich diese Informationen in Zeile Datum innerhalb der Datenbank? Schätze jede Hilfe.Erfassen der Datumszeit aus der Ansicht in die Datenbank MVC

-Controller

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

    } 

Zahlungen Klasse

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

Pay Ansicht

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

Ihr 'TransactionDate' hat einen Validator für reguläre Ausdrücke, der an eine bestimmte Kultur und Formatierung gebunden ist. Ist das beabsichtigt? Sind Sie bereit, ein ansonsten gültiges 24-Stunden-Datum/Uhrzeit-Format als ungültig abzulehnen? – Dai

+0

Es funktionierte nach dem Entfernen der regulären Ausdruck und danke für Ihre Antwort – Dodi

Antwort

2

konnte nicht Sie setzen es in der Steuerung:

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

Datum hinzugefügt, aber die Transaktion hat nicht funktioniert? Die Nummer, die ich in das Textfeld geschrieben habe, hat die Daten nicht an die Tabelle gesendet? – Dodi

+0

@Dai kann auf der Strecke sein. Ein Datumswert enthält kein Format. Ihre Regex könnte vielleicht auf eine Textbox angewendet werden, aber nicht auf die Eigenschaft selbst. – Gustav

+0

Wie ändere ich bitte? – Dodi

0
[HttpPost] 
public ActionResult Pay(Payments payment) 
{ 
    var d=DateTime.Now.ToUniversalTime(); 
    if(ModelState.IsValid) 
    { 

     Payments p=new() Payments{ 
     AccountNumber=p.AccountNumber, 
     Amount=payment.Amount, 
    TransactionDate=d 
}; 

     DB.Payment.Add(p); 
     DB.SaveChanges(); 
Verwandte Themen