0

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();

    }
Prashant Ghimire
  • 4,890
  • 3
  • 35
  • 46

2 Answers2

1

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 ;)

TomTom
  • 61,059
  • 10
  • 88
  • 148
  • I was thinking AJAX could come handy but I thought I might write in the server using loop one the user clicks submit button. I will try using Ajax and I hope it should work. :) – Prashant Ghimire Mar 07 '14 at 08:17
  • 1
    The problem is that this means pressing the submit button - which is contrary to what you ask for (immediate when value changes). When you learn ajax, also learn the jquery parts - hard to get in but VERY VERY handy. I start doing more and more small processing in the website. – TomTom Mar 07 '14 at 08:18
  • Thanks for suggestion buddy. First time I'm gonna use Ajax with MVC, haha :) – Prashant Ghimire Mar 07 '14 at 08:20
  • 1
    I was in the exasct same issue last week - and I have to say I do not like javascript. But it is very handy. – TomTom Mar 07 '14 at 08:35
0

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

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

Community
  • 1
  • 1
Marcin
  • 3,232
  • 4
  • 31
  • 48