Prashant G
Prashant G

Reputation: 4900

Updating list of models on the database ASP.NET MVC 5

I have a list of models that are displayed on the views. Say they are a list of Task. The list has a check button where one can check if Task is done. I want them to be updated on the database at once. How would I do that ?

Here's how my controller looks like :

public async Task<ActionResult> TaskManager(Tasks model, string id)
    {
        var user = await UserManager.FindByIdAsync(id);
        var list = from task in context.Tasks.Where(t=> t.isAssigned == false)
                   select task;

        foreach(var t in task)
        {
            t.isAssigned =(bool) // FROM EACH LIST ITEM IN THE VIEW ;
        }

        await context.SaveChangesAsync();
        return View();

    }

Upvotes: 0

Views: 828

Answers (2)

TomTom
TomTom

Reputation: 62093

button where one can check if Task is done. I want them to be updated on the database at once. How would I do that ?

You look at the wrong place. It isn not by doing "normal" MVC but by using Ajax.

User presses checkbox, a callback is made to a Api method (REST method) that does not return a HTML page but juts 200 - OK. It gets the ID as parameter and updates the task in the database. Right there, immediatly when the value changes.

Basically if you want to react to a checkbox change, you must do so - and it gets inefficient and slow to refresh the whole page then. This is why Microsoft invented many many year ago Ajax (before others even came up with the word). And it is well supported.

This is the moment you have to byte the not totally sweet apple for some of us and learn some javascript / JQuery to run in the browser. Been there last week, it is not THAT hard ;)

Upvotes: 1

Marcin
Marcin

Reputation: 3262

Entity Framework is not supporting by default Batch processing. There are few possibilities for that. See this comment:

https://stackoverflow.com/a/13024320/1164761

Upvotes: 0

Related Questions