ajax 调用控制器操作,将对象作为参数
本文关键字:对象 参数 调用 控制器 操作 ajax | 更新日期: 2023-09-27 18:37:11
我有一个ajax
调用ASP.NET
MVC
解决方案上的控制器,如下所示:
$.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 开始,现在我想用我存储在 globalVariable 上的信息调用一个不同的控制器,这些信息在我的 JavaScript 中定义如下:
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上检查了该变量是否包含我正在寻找的所有数据。实际上,如果我只传递对象的一个属性,它就可以进入控制器,但我无法传递整个对象......
尝试创建自己的自定义模型,而不是在操作中使用 JsonResult 作为参数,并在 ajax 调用中使用它:
$.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.stringify 以这种方式序列化您的对象:
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)
}
})
假设这是一个 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) { ... }
}
另一个可以更灵活地控制对象的创建和填充参数的选项是使用 IModelBinder 接口和 [ModelBinder] 属性。
您可以使用唯一的方法"BindModel"实现 IModelBinder 接口,以便在调用控制器方法之前访问 HTTP 请求,创建对象,读取 URL 查询参数,设置对象中的值,最后将其分配给 bindingContext 的 Model 属性:
一个简单的例子是这样的:
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