asp.net mvc 3 - MVC3 Dropdown repopulates fine even on error postback, but suggestion on how to make it efficient - without introducing error -


i can't post code @ moment on road , out of office. here code simplified. don't think have syntax issue such - think need clarification or, pointer.

model:  [validaton attribute] class data {    [required]    int xx   .. }  viewmodel:  class vm {   selectitem items;   int itemid;    data dt = new data();   public vm   {        items =...  load db...    }  } view  @textbox xx (etc) @dropdownfor(vm.itemid, vm.items  controller  void index {  vm vm = new vm(); //i default items here works - nice form loaded.   return view(vm); }  [post] void index(vm stuffposted) {  if modelstate.ok)( {      good. direct page redirect action }  else {     return view(stuffposted)     //this problem case need efficiency } 

problem

the form loads fine index method. on postback submit, id comes correctly in itemid. if there error - modelstate sends view...and selection list(s) in place, selections. oh joy! but, in postback method, vm stuffposted instance class invokes constructor , there (expensive) db call....to reload collections..then 'viewstate' seems load selected ids on top of it...

if refactor code there no expensive reload of collections...then classic error along lines of int32 expecting ienumerable , original form not show ...

i missing something, clearly..how reload collection 'viewstate' (page) reuse in error situation, , not need db call.

you use sessionstate avoid database trip repopulate dropdown. populate sessionstate on first request, , load when need repopulate dropdown (on error) on post. wary of size of collection storing in sessionstate - may better going database after re-load data.


Comments

Popular posts from this blog

linux - Mailx and Gmail nss config dir -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -