© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•4mo ago•
20 replies
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);
    }
}
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

✅ Asp.Net Help
C#CC# / help
3y ago
✅ Need Asp.net Developer
C#CC# / help
8mo ago
❔ ASP.NET Razor pages, understanding data
C#CC# / help
3y ago