c# - Different Timer for each item in List on ViewModel -
i have list of actions need performed company representatives , open timer if on edit x minutes. , 10 minutes page refreshed.
the problem many company agent can access , edit action. colored item on view, if agent "sleepee" x minutes - underprocess field reset false;
so start adding model field tell me action "underprocess" (i love codefirst :) , add viewmodel static dictionary on edit thare , add row kay-actionid , datetime of edit. after in view on foreach if item.underprocess style=color:red .. course after edit remove dictionary , change bool flag.
but not working - stay red after x minutes, think problem on static dictionary because when debug empty.
hare viewmodel:
public class agentactionviewmodel { public supplieraction action { get; set; } public list<supplieraction> actions { get; set; }//= new list<supplieraction>(); public static dictionary<int, datetime> underprocessfrom; public static agentactionviewmodel() { underprocessfrom = new dictionary<int, datetime>(); } }
the controller:
index
[supplierauthorization] public actionresult agentaction(..) { response.addheader("refresh", (minutes*60).tostring()); list<supplieraction> actions = db.supplieractions.tolist(); bool flag = false; var collection = agentactionviewmodel.underprocessfrom.keys; foreach (var aid in collection) { if (agentactionviewmodel.underprocessfrom[aid].addminutes(minutes) <= datetime.now) { agentactionviewmodel.underprocessfrom.remove(aid); var action = actions.find(a=>a.supplieractionid == aid); action.underprocess = false; db.entry(action).state = entitystate.modified; } } if(flag) db.savechanges(); ... var vm = new agentactionviewmodel(); vm.actions = actions.orderbydescending(d => d.requestdate).tolist(); return view(vm); }
get edit:
[supplierauthorization] public actionresult edit(int id = -1) { try { ... if (!a.underprocess) { a.underprocess = true; db.entry(a).state = entitystate.modified; db.savechanges(); agentactionviewmodel.underprocessfrom.add(id, datetime.now); } ... } catch (exception ex) { .... if (a.underprocess) { a.underprocess = false; db.entry(a).state = entitystate.modified; db.savechanges(); agentactionviewmodel.underprocessfrom.remove(id); } ..... } ... }
post edit :
..... if (vm.action.underprocess) { vm.action.underprocess = false; agentactionviewmodel.underprocessfrom.remove(vm.actionid); } .....
my view :
@model occ.iptogo.viewmodel.agentactionviewmodel .... <tbody> @foreach (var item in model.actions) { <tr style="@(item.underprocess ? "color:red" : "")"> <td> .....
10x sorry bad english =)
using static memory repository in web application bad idea, if repository short term.
the main reason memory repository cannot scaled - if run server in multi-process topography, or multi-machine topography - memory not shared, timers not shared either.
also, when server restarted - memory gone it, , lose information.
i suggest use more scallable repository, maybe memcached
if don't part of database.
at least, make easier debug problem.
Comments
Post a Comment