2016-07-07 72 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中的工作方式。 –