2016-03-03 31 views
0

我有一個ASP.NETMVC解決方案的ajax調用一個控制器,看起來像這樣:AJAX調用控制器動作用一個對象作爲參數

$.ajax({ 
     url: "ControllerClass/ControllerMethod?date=" + date, 
     type: "POST", 
     dataType: 'json', 
     contentType: 'application/json; charset=utf-8', 
     success: function (result) { 
      globalVariable = result; // This is for later... 
      DoSomething(result); 
     }, 
     async: true, 
     processData: false 
    }) 

一旦控制器已完成了服務器上所有工作側時,它返回其包含不同的屬性類型(一個Int,int數組和對象列表)

方式的對象,我返回從控制器回JS文件數據是...

return Json(new 
{ 
    summary = objectResult 
}); 

的一點是,從JavaScript的,現在我想打電話與在我的JavaScript這樣定義的,我已經保存在我的全局變量的信息,不同的控制器:那VAR位於

var globalVariable 

在我的JS文件的頂部...

嗯,我想再打我的控制器與該變量的方式是這樣的:

$.ajax({ 
       url: "ControllerClass/ControllerMethod?result=" + globalVariable, 
       type: "POST", 
       dataType: 'json', 
       contentType: 'application/json; charset=utf-8', 
       success: function (result) { 
        DoSomethingNewWithThisresult(result) 
       }, 
       async: true, 
       processData: false 
      }) 

這是我的控制器在C#中。

public IActionResult ControllerMethod(JsonResult result) 
{ 
    DoSomethingHereWithTheResult(result);  
} 

爲什麼如果我在最後一個控制器上放置一個斷路器,結果變量是空的?我檢查了Chrome,該變量包含了我正在尋找的所有數據。實際上,如果我只是通過對象的屬性之一,它會轉到控制器上,但我無法傳遞整個對象...

回答

1

嘗試創建你的O WN自定​​義模型,而不是在Ajax調用使用JsonResult如在動作參數和使用這種方式:

$.ajax({ 
      url: "ControllerClass/ControllerMethod", 
      type: "POST", 
      dataType: 'json', 
      data: { Summary: globalVariable.summary}, 
      contentType: 'application/json; charset=utf-8', 
      success: function (result) { 
       DoSomethingNewWithThisresult(result) 
      } 
     }) 


public class YourClass 
{ 
    public string Summary { get; set;}//// string type or whatever it is 
} 

public IActionResult ControllerMethod(YourClass result) 
{ 
    DoSomethingHereWithTheResult(result);  
} 

或者你也可以使用JSON。以這種方式串行化你的對象:

var customObject={ 
"Summary" : globalVariable.summary 
}; 

    $.ajax({ 
      url: "ControllerClass/ControllerMethod", 
      type: "POST", 
      dataType: 'json', 
      data: JSON.stringify(customObject), 
      contentType: 'application/json; charset=utf-8', 
      success: function (result) { 
       DoSomethingNewWithThisresult(result) 
      } 
     }) 
+0

這正是我需要什麼!我沒有意識到Ajax上的數據屬性:)謝謝! – user3587624

0

假設這是一個Web API控制器,有多種方法從URL中讀取一個複雜類型 - (也有類似的MVC控制器選項)

如果參數在UrL中傳遞,並假設它們以1:1映射到對象,則可以用[ FromUri]屬性,例如:

public class GeoPoint 
{ 
    public double Latitude { get; set; } 
    public double Longitude { get; set; } 
} 

public ValuesController : ApiController 
{ 
    public HttpResponseMessage Get([FromUri] GeoPoint location) { ... } 
} 

另一個選項,可以更靈活地控制對象的創建並填充pa rameters是通過使用IModelBinder接口和[ModelBinder]屬性。

您可以通過唯一的方法「BindModel」實現IModelBinder接口,以便在調用Controller方法之前訪問HTTP請求,創建對象,讀取URL查詢參數,設置對象中的值,然後最終分配它的BindingContext的模型屬性:

一個簡單的例子是這樣的:

public bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext) 
{ 
    if (bindingContext.ModelType == typeof(MyComplexType)) 
    { 
     MyComplexType model ; 

     //You can use the value provider or get it from the HttpRequest 
     //var theValue = bindingContext.ValueProvider.GetValue (bindingContext.ModelName); 
     //var theValue = request.RequestUri.ParseQueryString () ; 
     if (new MyComplexTypeConverter().TryParse (actionContext.Request, out model)) 
     { 
     bindingContext.Model = model; 

     return true; 
     } 
     else 
     { 
     bindingContext.ModelState.AddModelError(bindingContext.ModelName, "Cannot convert request to MyComplexType"); 

     return false; 
     } 
    } 

    return false ; 
} 

這裏是一個參考文檔:http://www.asp.net/web-api/overview/formats-and-model-binding/parameter-binding-in-aspnet-web-api

+0

也許我誤解了這些信息,但我認爲這不是我真正需要的。我並不需要收集來自URL,但通過第一AJAX調用執行第一次調用我的控制器之後,我產生的JSON數據的參數。我的URL不包含在任何時候任何參數,順便說一句。謝謝! – user3587624

+0

在你的問題要附加的全局變量的Ajax調用像這樣的網址:網址:「ControllerClass/ControllerMethod結果=?」 +全局變量,你的結果的參數值是需要被映射到控制器參數 –

相關問題