0

I am confused and want to know the best practice for the use same information in multiple action methods. I have five values that I want to use in multiple action methods. I have model class which have that five values.

Currently I am storing data into session and getting that data in every action method where I need It.

[HttpPost]
public ActionResult Index(Model model){
    Session["A"]=mode.a;
    Session["B"]=mode.b;
    Session["C"]=mode.c;
    Session["D"]=mode.d;
    Session["D"]=mode.d;
}

[HttpGet]
public ActionResult Action1(){
    Label1.Text=Session["A"];
    Label2.Text=Session["B"];
    Label3.Text=Session["C"];
    Label4.Text=Session["D"];
    Label5.Text=Session["E"];
}

[HttpGet]
public ActionResult Action2(){
    Label1.Text=Session["A"];
    Label2.Text=Session["B"];
    Label3.Text=Session["C"];
    Label4.Text=Session["D"];
    Label5.Text=Session["E"];
}

So simply my question is Can I pass that object of model class in every action method? Or there is any other best practice to use that object of the model class in every action method or using the session is better way ?

My all action method's views uses the same `model class.

1 Answer 1

2

You can store the model itself within the Session, rather than storing each property of that object, makes storage a little easier perhaps.

Session["myModel"]=model;

Label1.Text=((Model)Session["myModel"]).a;

Without persistence of that information outside of the Session, there would be no other way to access the object on multiple, distinct GETs

Sign up to request clarification or add additional context in comments.

4 Comments

But session may be effect the server and app performance. If there is 1M user is current accessing the website and I storing that data into session so the load on the server may be increased. No doubt the in session I am storing maximum 1KB data.
If you intend to have that many users, you will need to use some kind of shared storage for Session regardless as it is highly unlikely that you will be able to run it from a single server. You can run Session information from a database and the impact of doing so is unlikely to be anything larger than a custom persistence implementation.
Redis cache session state provider is better option for shared and persistence session for the large number of users. Find the detail implementation on link azure.microsoft.com/en-us/documentation/articles/…
That would depend if users want to hook up to azure, which I believe is where this lives. Great option if you are there, another dependency that can be implemented simply on a standard server set-up if you are not.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.