Home Ask Login Register

Developers Planet

Your answer is one click away!

Subpar Web Dev February 2016

In my controller class, where do I keep a variable that is associated with a page?

Allow me to explain my situation in more detail. I have a controller like

public class AnswersController : Controller
{
    // ...
    private Guid _Pid;
   // ... 
    [HttpGet]
    public ActionResult FillOut ( Guid pid )
    {
        this._Pid = pid;
        // ...
    }
    // ...
 }

which is my attempt at stored a variable _Pid associated with the view invoked by FillOut so that when an AJAX call from the view invokes another method on the controller that uses the variable

    [HttpPost]
    public ActionResult SubmitAnswers ( List<AnswerSubmission> Answers )
    {
        // ... 
        this._Db.SubmitAnswers(Answers, this._Pid);

I have it there instead of passing it to the page and posting it back. But this is resulting in errors due to this._Pid not being set.

Where is the flaw in my reasoning? Do I need to pass this variable to the view? I thought that a new instance of a controller was associated with a page.

Answers


Shyju February 2016

Http is stateless ! That means, every http request you make to your code has no idea what happened with your previous http request. So when you make the second ajax call, the second request is going to create a totally new object of your controller and initialize your variables.

What you should do is, pass the Guid value back to your view and when you make the ajax call, Use it again. For this, you may add a new property for your page view model.

public class FillOutVm
{
  public Guid Pid { set;get;}
  //Other properties

}

and in your Get action, set this property value.

[HttpGet]
public ActionResult FillOut ( Guid pid )
{
  var vm= new FillOutVm { Pid=pid };
  return View(vm);
}

And in your view which is strongly typed to this view model, you may keep this value in a hidden field

@model FillOutVm
@Html.HiddenFor(s=>s.Pid)

and in your js code which makes the ajax call, read the value of this input field and use it and pass it as the data you are sending from the ajax call.

var pid=$("#Pid").val();
//use pid variable value in your ajax call now

You need to add a parameter to your action method to accept this pid value.

[HttpPost]
public ActionResult SubmitAnswers ( List<AnswerSubmission> Answers,Guid pid )
{
   //  do something with the passed values
}

Another option is to store the value in db table/ Session state etc.


Gusman February 2016

The flaw in your reasoning is that each query to the server creates a new controller, so each time _Pid is null.

You have a good choice and a fast and dirty choice: store the data in a physical storage (file, db, etc) or make the property static and it will be shared among all the instances.

But beware, the static field can be emptied some times because the AppDomain can be unloaded by the server.


Oleg Sh February 2016

Pass value of _Pid to View and then use it as parameter in your ajax request. ASP.NET MVC create new object of your controller (with init values) for each request.

Post Status

Asked in February 2016
Viewed 3,153 times
Voted 11
Answered 3 times

Search




Leave an answer


Quote of the day: live life