C
C#2mo ago
strikeouts27

✅ I need help understanding ASP.NET savechanges()

I am trying to understand my professors expectations on where I am supposed to place this file. Is this code for a controller?
[HttpPost]
public IActionResult Edit(Contact contact)
{
if (ModelState.IsValid)
{
if (contact.ContactId == 0)
{
contact.DateAdded = DateTime.Now;
context.Contacts.Add(contact);
}
else
{
context.Contacts.Update(contact);
}

context.SaveChanges(); // Writes changes to database

return RedirectToAction("Index", "Home");
}
else
{
return View(contact);
}
}
[HttpPost]
public IActionResult Edit(Contact contact)
{
if (ModelState.IsValid)
{
if (contact.ContactId == 0)
{
contact.DateAdded = DateTime.Now;
context.Contacts.Add(contact);
}
else
{
context.Contacts.Update(contact);
}

context.SaveChanges(); // Writes changes to database

return RedirectToAction("Index", "Home");
}
else
{
return View(contact);
}
}
8 Replies
strikeouts27
strikeouts27OP2mo ago
editing
Jimmacle
Jimmacle2mo ago
nitpick: you're asking about EF Core which has no direct relation to ASP.NET Core also, $code
MODiX
MODiX2mo ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat For longer snippets, use: https://paste.mod.gg/
Jimmacle
Jimmacle2mo ago
this exact code is a method that looks like it should be defined in a controller, yes
strikeouts27
strikeouts27OP2mo ago
Thank you Jimmacle.
MODiX
MODiX2mo ago
Please don't upload any potentially harmful files @strikeouts27, your message has been removed
Unknown User
Unknown User5w ago
Message Not Public
Sign In & Join Server To View
MODiX
MODiX5w ago
If you have no further questions, please use /close to mark the forum thread as answered

Did you find this page helpful?