2016-07-07 71 views
0

Ajax調用在我的控制器之間得到復位,返回查看該方法還初始化一些值,一些類級別的屬性:價值控制器

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); 
} 

此外,有這種方法,它被調用到數據綁定到頁面上的網格:

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被設置爲當創建視圖true,但是當數據被綁定到網格isSuggested設置爲false出於某種原因。

我的網格是在剃刀視圖中定義像這樣:

@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) 

.ReadSource("Controller", "GetSelectedCodesInfo")位是什麼是指控制器和控制器上的方法調用。所以,它調用了上面的第二個代碼片段。

我必須訪問我的Controller類的兩個單獨的實例,但我不知道如何解決此問題。我怎樣才能做到這一點?我怎麼能讓我的網格通過Codes對象的引用?然後,我可以從那裏獲取網格值...

回答

1

這是預期的行爲。 isSuggested是一個類級別的變量。每次發出Http請求時,都會創建一個新的控制器實例。這意味着變量將被初始化爲false。請記住,Http是無狀態的 :)

如果要在多個http調用之間保留一個變量值,則需要保留它。你有一個像

  1. 不同的選擇堅持到數據庫表,並從第二呼叫
  2. 寫在磁盤讀取的文件,並在第二個電話
  3. 從讀取保存到用戶會話和閱讀從第二個電話
+0

謝謝;我懷疑這是由設計。我只是不知道如何處理它。我可能會按照你的建議把它放到會話變量中。如果有效,我會接受你的回答。 – sab669

+0

那麼,你的解決方案解決了我的問題......但不幸的是它自身存在問題。雖然謝謝! – sab669

+0

4.通過HTTP Post的隱藏字段來回傳遞,類似於ViewState在asp.net中的工作方式。 –