c# - How to improve DRY in Create GET and POST actions in an ASP.NET MVC controller -


i'm looking how can make mvc code more efficient, reusing same code in create , post actions goes against dry principles of mvc.

specifically i'm using entityframework , have controller populates viewmodel following code:

public actionresult create()     {          var filemanagers = x in db.userprofiles                             select x;          var estimators = x in db.userprofiles                                 select x;          var projectmanagers = x in db.userprofiles                             select x;          var jobstatuses = x in db.jobstatuses                             select x;                     jobviewmodel viewmodel = new jobviewmodel         {             job = new job(),             filemanagers = filemanagers.select(x => new selectlistitem             {                 value = x.username,                 text = x.firstname + " " + x.lastname             }).tolist(),             estimators = estimators.select(x => new selectlistitem             {                 value = x.username,                 text = x.firstname + " " + x.lastname             }).tolist(),             projectmanagers = projectmanagers.select(x => new selectlistitem             {                 value = x.username,                 text = x.firstname + " " + x.lastname             }).tolist()         };          return view(viewmodel);     } 

in post function, i'm checking model validity, , writing out same code again repopulate viewmodel if model not valid, shown in asp.net music tutorial @ http://www.asp.net/mvc/tutorials/mvc-music-store/mvc-music-store-part-5.

    [httppost]     [validateantiforgerytoken]     public actionresult create(job job, string action)     {          if (modelstate.isvalid)         {             db.jobs.add(job);             db.savechanges();             return redirecttoaction("index");         }          var filemanagers = x in db.userprofiles                            select x;          var estimators = x in db.userprofiles                          select x;          var projectmanagers = x in db.userprofiles                               select x;          var jobstatuses = x in db.jobstatuses                           select x;          jobviewmodel viewmodel = new jobviewmodel         {             job = job,             filemanagers = filemanagers.select(x => new selectlistitem             {                 value = x.username,                 text = x.firstname + " " + x.lastname             }).tolist(),             estimators = estimators.select(x => new selectlistitem             {                 value = x.username,                 text = x.firstname + " " + x.lastname             }).tolist(),             projectmanagers = projectmanagers.select(x => new selectlistitem             {                 value = x.username,                 text = x.firstname + " " + x.lastname             }).tolist()         };          return view(viewmodel);      } 

can please suggest better way of doing this? preferably without adding seperate orm layer, project quite small.

just put reused code in method , call both action methods, like

  public actionresult create()         {              return view(createviewmodel(new job()));         }          [httppost]         [validateantiforgerytoken]         public actionresult create(job job, string action)         {              if (modelstate.isvalid)             {                 db.jobs.add(job);                 db.savechanges();                 return redirecttoaction("index");             }                          return view(createviewmodel(job));         }          private jobviewmodel createviewmodel(job job)         {             var filemanagers = x in db.userprofiles                                select x;              var estimators = x in db.userprofiles                              select x;              var projectmanagers = x in db.userprofiles                                   select x;              var jobstatuses = x in db.jobstatuses                               select x;              jobviewmodel viewmodel = new jobviewmodel             {                 job = job,                 filemanagers = filemanagers.select(x => new selectlistitem                 {                     value = x.username,                     text = x.firstname + " " + x.lastname                 }).tolist(),                 estimators = estimators.select(x => new selectlistitem                 {                     value = x.username,                     text = x.firstname + " " + x.lastname                 }).tolist(),                 projectmanagers = projectmanagers.select(x => new selectlistitem                 {                     value = x.username,                     text = x.firstname + " " + x.lastname                 }).tolist()             };              return viewmodel;         } 

its anyway, save typing same stuff twice


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -