Harjinder Randhawa
Harjinder Randhawa

Reputation: 27

Insert date from view to controller

I am filling a form in "view" and sending it back to "controller" class. In my database i want to add the date and time of user creation.

code in my view

<div class="form-group">
        @Html.LabelFor(model => model.Ticket_CreationDate, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Ticket_CreationDate)
            @Html.ValidationMessageFor(model => model.Ticket_CreationDate)
        </div>
    </div>

code in controller class

public ActionResult Create([Bind(Include = "Ticket_SenderEmail,Ticket_Sujet,Ticket_Body,Ticket_CreationDate,Ticket_ClientID,Ticket_Priority,Ticket_UserID,Ticket_Status_ID")] Ticket ticket)
    {
        if (ModelState.IsValid)
        {
            db.Ticket.Add(ticket);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        ViewBag.Ticket_ClientID = new SelectList(db.Client, "Client_Id", "Client_Nom", ticket.Ticket_ClientID);
        ViewBag.Ticket_Priority = new SelectList(db.Priority, "Priority_Id", "Priority_Name", ticket.Ticket_Priority);
        ViewBag.Ticket_Status_ID = new SelectList(db.Ticket_Status, "Ticket_Status_Id", "Ticket_Status_Nom", ticket.Ticket_Status_ID);
        ViewBag.Ticket_UserID = new SelectList(db.User, "User_Id", "User_Nom", ticket.Ticket_UserID);
        return View(ticket);
    }

My question is how i can send current datetime from view to controller ?

Upvotes: 0

Views: 333

Answers (1)

Ethan Pelton
Ethan Pelton

Reputation: 1796

You won't send the date from the view, you will handle that in the controller...

 yourdataentity newentity = new yourdataentity();

 //the rest of your implementation may be a bit different, but this is one way to set the date for a new record...
 newentity.createDate = DateTime.Now;

 yourentitys.Add(newentity);

 if (ModelState.IsValid)
 {
      db.yourentitys.Add(newentity);
      db.SaveChanges();
 }

Upvotes: 1

Related Questions