2016-03-03 30 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结果=?” +全局变量,你的结果的参数值是需要被映射到控制器参数 –

相关问题