简体   繁体   中英

Asp.Net Core 3.1 Data is retrieved but not saved into database

I am creating an Asp.Net core web application. I have successfully connected database to my app. I can run all the migration successfully and even retrieve data from database. But when I try to save data using my DbContext data is not saved in the database. Here is my code

Category Model Class

public class Category
{
    public int Id { get; set; }
    public string Name { get; set; }
}

Category Controller

public class CategoryController : Controller
{
    private readonly AppDbContext dbContext;

    public CategoryController(AppDbContext dbContext)
    {
        this.dbContext = dbContext;
    }

    [HttpGet]
    public IActionResult Index()
    {
        var categories = dbContext.Categories.ToList();
        return View(categories);
    }

    [HttpGet]
    public IActionResult Create()
    {
        return View();
    }

    [HttpPost]
    public IActionResult Create(Category category)
    {
        if (ModelState.IsValid)
        {
            dbContext.Categories.Add(category);
            return RedirectToAction(nameof(Index));
        }
        return View();
    }
}

View

<form asp-action="Create">
        <div asp-validation-summary="ModelOnly" class="text-danger"></div>
        <div class="form-group">
            <label asp-for="Name" class="control-label"></label>
            <input asp-for="Name" class="form-control" />
            <span asp-validation-for="Name" class="text-danger"></span>
        </div>
        <div class="form-group">
            <input type="submit" value="Create" class="btn btn-primary" />
        </div>
 </form>

DbContext Class

public class AppDbContext : DbContext
{
    public AppDbContext(DbContextOptions<AppDbContext> options) : base(options)
    {

    }

    public DbSet<Category> Categories { get; set; }
}

and finally my ConfigureServices Method in Startup file

public void ConfigureServices(IServiceCollection services)
    {
        services.AddControllersWithViews();

        services.AddDbContext<AppDbContext>(options => options
        .UseLazyLoadingProxies()
        .UseSqlServer(Configuration.GetConnectionString("AppDbContext")));
    }

I successfully get data in the Index method that I have manually stored in database but cannot save from a form.

It is necessary to call SaveChanges() method of EntityFramework to save data:

dbContext.Categories.Add(category);
dbContext.SaveChanges();

It is necessary to call SaveChanges after creating, updating, deleting operations.

In Entity Framework, the DbContext.SaveChanges() method saves all changes made in the context of the database.

So after adding, updating or deleting data you need call SaveChages() method:

dbContext.Categories.Add(category);
dbContext.SaveChanges();

for knowing more about SaveChange() you can follow this link .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM