asp.net mvc - MVC4 load data into partial views -


my main (myprofile) view contains links when user clicks on link partial view loads in div existing data db can updated user.

@ajax.actionlink("update 1", "update1", new { email = @viewbag.email }, new ajaxoptions() { httpmethod = "get", updatetargetid = "divcurrentview", insertionmode = insertionmode.replace })   @ajax.actionlink("update 2", "update2", new { email = @viewbag.email }, new ajaxoptions() { httpmethod = "get", updatetargetid = "divcurrentview", insertionmode = insertionmode.replace })    <div id="divcurrentview">  </div> 

partial views: example:

_update1:

@model viewmodels.update1 @using (html.beginform()) {  @html.antiforgerytoken()  @html.validationsummary(true)                                 @html.hiddenfor(model => model.id)   @html.labelfor(model => model.name)   @html.textboxfor(model => model.name)  <input type="submit" value="update" /> } 

_update2:

 @model viewmodels.update2  @using (html.beginform())  {   @html.antiforgerytoken()   @html.validationsummary(true)                                  @html.hiddenfor(model => model.id)    @html.labelfor(model => model.website)   @html.textboxfor(model => model.website)     <input type="submit" value="update" />  } 

in controller:

public partialviewresult update1(string email)     {          var model = populate viewmodel         return partialview("_update1",model);     } public partialviewresult update2(string email)     {          var model = populate viewmodel         return partialview("_update2",model);     } 

it doesnt mean user click on links when accessing main view.

i want feedback if way correct or should load data once when user gets myprofile view , store data in session , when each partial view gets loaded data gets loaded session? avoid calling db every time partilaview gets loaded or there better approach?

thanks,

update:

i tried use cache suggested problem data store globally. if multiple users login , try view/update data, data identical of them missing something?

this tried:

  public partialviewresult update1(string email)     {         var cc = httpruntime.cache.get("cinfo");         update1vm model = null;         if (cc == null)         {             model = populate viewmodel             httpruntime.cache.insert("cinfo", model);         }         else         {             model = (update1vm)cc;         }          return partialview("_update1", model);     } 

use html.actionlink in way use solution. makes controller simple , reuseble. can add , remove views without changes in controller. easy put them in diffrent view. it's more flexible solution.

if afraid of calling db every time can use caching, in example query db when user need , click it.

if put in 1 view more complicated, more messy , less error prone.


Comments

Popular posts from this blog

php - Submit Form Data without Reloading page -

linux - Rails running on virtual machine in Windows -

php - $params->set Array between square bracket -