dimanche 1 mai 2016

Working with tempdata in mvc

As I have read that once you set a tempdata, it will last till next request and if you need to use it more then we need to use keep. But in this situation there is somewhere my tempdata is being lost.

The scenario is below:

I have a view and corresponding to that I have a action method in my controller and here I just set a tempdata as below:

Controller class:

public actionresult myview()
{
   tempdata["Empid"]= sourceid;
} 

The view consists of several renderAction as below and all these actionmethods in controller return some partialviews

@html.renderAction("details","mycontroller")
@html.renderAction("details","mycontroller")
@html.renderAction("details","mycontroller")
@html.renderAction("details","mycontroller")

Now in partialviews, I have several ajaxified calls suppose in one of the partialview , I have a post method using ajax like below:

$.post("action", "controller",{}});

Please ignore syntactical mistakes as I only have problem in understating the logic of tempdata.

So, coming to above, now the post methods have actionmethods that uses the tempdata that i have set when my view page loads i.e tempdata["Empid"] because this tempdata is needed to get details of employee.

So, it gets the data, reurn json formatted data and I show it in view perfectly. Uptill here everything is fine. Now, I have one of the partialviews that uses the ajax post to hit controller which is not the same controller but a different cs file. Here this tempdata goes off.

Why is it happening so..

Aucun commentaire:

Enregistrer un commentaire