Jon Bellamy

Jon Bellamy

  • NA
  • 47
  • 0

MVC3 Controller and fields

May 22 2011 10:03 AM
Hi All,

Once again, despite hours of trying to find a solution on Google :-) I find my abilites tested through MVC3.  MVC3 is very new to me, and I have yet to discover a good book that takes you through a step-by-step approach to learn all aspects to a basic level and so am forced to patch everything together as best as I can and improve methodology bit by bit.  I have a particular problem and hope that someone can help me.

I have a controller at which I have declared the following:

public int CurrentProfile = 0;

I then have the following method on the controller:

public ActionResult SetProfile (int pId)
{
        CurrentProfile = pId;
}

I have two comboboxes on the UI, the first contains "Countries" and the second contains "Profiles".  When the UI (View) loads, the first is populated with countries (based on the user logged in) which in turn when selected populated the second one with the profiles that belong to that country.  This works absolutely fine.

When a profile is selected, the View through JQuery .getJSON passes the value of the selected profile back to the controller at which point the view displays data appropriate to the profile.

The oddest of problems is occuring.  When the View first loads, the frst country is selected automatically (which in turn auto-selects the first profile for that country, which in turn auto-posts the profile id to the controller to update the field CurrentProfile).  The correct data is loaded.  Say for example that the id is 1.

When I then change the profile, the same happens again, the profile id is passed the the SetProfile method and the CurrentProfile updated.  Let's say it's now 3.  The data updates fine.

However, if I then re-select the first profile, the id of 1 is passed back to CurrentProfile through SetProfile and the value is updated, but when the method to re-draw the data on the view is called, it saysthe the id of 3 and not 1 - I have checked, double-checked, and checked again to ensure that it is being set correctly.

There appears to be no logical explanation (other than that I am clearly missing a key point of MVC).  I assume it may have something to do with state but am really clueless.

Please help, thank you in advance, Jon

Answers (2)