sab669 sab669 - 4 months ago 11
Ajax Question

Value getting reset between ajax calls in Controller

In my controller, the method that returns the View also initializes a few values for some class-level properties:

private string igc = String.Empty;
private string igcCode = String.Empty;
private bool isSuggested = false;

public ActionResult Codes(Codes objCodes)
{
try
{
FillDropDowns(objCodes);
igc = String.Empty;
if (objICDCodes.FromWhere.IndexOf("MedicalInfo-Suggested") >= 0)
{
igc = objCodes.FromWhere.Remove(0, "MedicalInfo-Suggested-".Length);
igcCode = igc.Substring(0, igc.IndexOf("-")).Trim();
objCodes.ICGCode = igcCode;
isSuggested = true;
}
}
catch (Exception ex)
{
//logging error
ElmahLogUtility.ErrorException(ex);
}

return View(base.GetViewPath("Codes"), objCodes);
}


Additionally, there is this method which gets called to bind data to a grid on the page:

public JsonResult GetSelectedCodesInfo(List<SearchField> searchFields, GridDataSourceRequest request)
{
//creating the instance of DataSourceResult.
DataSourceResult dataSourceResult = null;
try
{
// Creating the instance of CommonBLL to load the values.
CommonBLL objCommonBLL = new CommonBLL();

if (isSuggested)
{
searchFields.Add(new SearchField() { ElementName = "aIGCode", Value = igcCode });
searchFields.Add(new SearchField() { ElementName = "aFor", Value = "EtiologicDiagnosis" });
}

// Getting the Codes information and storing in the DataSource Result.
dataSourceResult = objCommonBLL.GetSelectedCodesInfo(searchFields, request);
}
catch (Exception ex)
{
//Logging the Exception
ElmahLogUtility.ErrorException(ex);
}

// Returning the Result.
return Json(dataSourceResult, JsonRequestBehavior.AllowGet);
}


isSuggested
gets set to
true
when the View is created, but when the data is bound to the grid
isSuggested
is set to
false
for some reason.

My grid is defined in a Razor view like so:

@Html.Grid("CodesSelectionGrid").ReadSource("Controller", "GetSelectedCodesInfo").OnGridDataBound("AssignCodeValues").Lazyload(true).EnableGrouping(false).EnableSorting(true).PageSize(10).Height("390px").Width("610px").EnablePaging(true).EnableFiltering(false).EnableMultiSelect(true).SelectionMode(SelectionMode.Single, "GetSelectedCodeDetails").RowSelection(GridRowSelection.None).ShowToolBar(true).SelectionCSSClass("icd-editable-cell").PageButtonCount(3)


That
.ReadSource("Controller", "GetSelectedCodesInfo")
bit is what refers to the Controller and the method on the controller to call. So, it's calling the second snippet of code above.

I must be accessing two separate instances of my
Controller
class, but I do not know how to solve this problem. How can I do this? How could I have my grid pass a reference of the
Codes
object? Then I could just get the values from there for the grid...

Answer

This is the expected behavior. isSuggested is a class level variable. Every time you make an Http request, a new instance of your controller will be created. That means the variable will be initialized to false. Remember, Http is Stateless :)

If you want to persist a variable value between multiple http calls, you need to persist it. You have different options like

  1. Persist to a database table and read from that in the second call
  2. Write to a file in disk and read from that in the second call
  3. Save to user session and read from that in the second call
Comments