public ActionResult Create(GB gb) { if (ModelState.IsValid) { gb.Id = Guid.NewGuid(); gb.PostTime = DateTime.Now; db.GBs.Add(gb); db.SaveChanges(); return(RedirectToAction("List")); } return(View(gb)); }
// Submit Button adds a new guestbook entry to the database, // clears the form and displays the updated list of guestbook entries protected void submitButton_Click(object sender, EventArgs e) { // use GuestbookEntities DbContext to add a new message using (GuestbookEntities dbcontext = new GuestbookEntities()) { // create a new Message to add to the database; Message is // the entity data model class representing a table row Message message = new Message(); // set new Message's properties message.Date = DateTime.Now.ToShortDateString(); message.Name = nameTextBox.Text; message.Email = emailTextBox.Text; message.Message1 = messageTextBox.Text; // add new Message to GuestbookEntities DbContext dbcontext.Messages.Add(message); dbcontext.SaveChanges(); // save changes to the database } // end using statement // clear the TextBoxes nameTextBox.Text = String.Empty; emailTextBox.Text = String.Empty; messageTextBox.Text = String.Empty; // update the GridView with the new database table contents messagesGridView.DataBind(); } // submitButton_Click
public void InsertGuestbooks(Guestbooks newData) { newData.CreateTime = DateTime.Now; db.Guestbooks.Add(newData); db.SaveChanges(); }