使用AJAX将动态数据传递给mvc控制器
How can I pass dynamic
data with an AJAX
call to an MVC
Controller
?
如何通过AJAX调用将动态数据传递给MVC控制器?
Controller
:
控制器:
public JsonResult ApplyFilters(dynamic filters){
return null;
}
The AJAX
call:
AJAX电话:
$(':checkbox').click(function (event) {
var serviceIds = $('input[type="checkbox"]:checked').map(function () {
return $(this).val();
}).toArray();
//alert(serviceIds);
$.ajax({
type: 'GET',
url: '/home/ApplyFilters',
data: JSON.stringify({
name: serviceIds
}),
contentType: 'application/json',
success: function (data) {
alert("succeeded");
},
error: function (err, data) {
alert("Error " + err.responseText);
}
});
//return false;
});
Ideally would be that the filters
would contain the serviceIds
as a property
理想情况下,过滤器将包含serviceIds作为属性
For example like this: filters.ServiceIds
. I got another filter for a date range and that one would be added like so: filters.DateRange
.
例如:filter.ServiceIds。我有一个日期范围的另一个过滤器,其中一个将添加如下:filters.DateRange。
And server side get the filter as a dynamic
object in the ApplyFilters()
服务器端将过滤器作为ApplyFilters()中的动态对象
1 个解决方案
#1
7
Ad far as i know the default ASP MVC model binder cannot accomplish such a thing.
据我所知,默认的ASP MVC模型绑定器无法完成这样的事情。
Fortunately there are numerous solutions, here are two of them :
幸运的是有很多解决方案,其中有两个:
1. Here is the first one, it converts your JSON data into a IDictionary<string, object>
instance before passing it to your controller action. You would end up with :
1.这是第一个,它将您的JSON数据转换为IDictionary
public JsonResult ApplyFilters(IDictionary<string, object> filters)
{
return null;
}
2. The second approach enables your controller action to receive a JsonValue instance. Making your action looks like this one :
2.第二种方法使您的控制器操作能够接收JsonValue实例。让你的行动看起来像这样:
public JsonResult ApplyFilters([DynamicJson]JsonValue filters)
{
return null;
}
3. Based on the previous tutorials, you could create your own custom model binder returning a dynamic object. The following code has been taken from the first proposal and modified to generate a dynamic object. It is just to illustrate the idea, don't use it as is :
3.根据之前的教程,您可以创建自己的自定义模型绑定器,返回动态对象。以下代码取自第一个提案并已修改以生成动态对象。它只是为了说明这个想法,不要按原样使用它:
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
if (!controllerContext.HttpContext.Request.ContentType.StartsWith("application/json", StringComparison.OrdinalIgnoreCase))
{
return null;
}
controllerContext.HttpContext.Request.InputStream.Position = 0;
using (var reader = new StreamReader(controllerContext.HttpContext.Request.InputStream))
{
var json = reader.ReadToEnd();
if (string.IsNullOrEmpty(json))
{
return null;
}
dynamic result = new ExpandoObject();
var deserializedObject = new JavaScriptSerializer().DeserializeObject(json) as IDictionary<string, object>;
if (deserializedObject != null)
{
foreach (var item in deserializedObject)
{
((IDictionary<string, object>)result).Add(item.Key, item.Value);
}
}
return result;
}
}
These solutions rely on building a custom model binder to handle this particuliar scenario :
这些解决方案依赖于构建自定义模型绑定器来处理这种特殊情况:
- Custom model binder is just an implementation of the IModelBinder interface in charge of parsing JSON posted data and converting them into another format.
- 自定义模型绑定器只是IModelBinder接口的一个实现,负责解析JSON发布的数据并将它们转换为另一种格式。
- ASP MVC framework is told to use these specific custom model binders by registering them (with
ModelBinders.Binders.Add
or using a dedicated attribute (CustomModelBinderAttribute). - ASP MVC框架被告知通过注册它们(使用ModelBinders.Binders.Add或使用专用属性(CustomModelBinderAttribute))来使用这些特定的自定义模型绑定器。
更多相关文章
- 聊聊css盒子模型
- AngularJS使用双向数据绑定将img元素标签中的图像显示为源?
- HTML+DIV+CSS零基础快速入门到制作企业站视频课程_18 盒模型[浮
- 【转】phpcms-v9中关于模型的理解
- PHPCMS V9 的手机门户wap绑定单页面
- Laravel 5 PDO绑定变量数与令牌数不匹配
- thinkphp5 数据库和模型详解 之1 数据库基础
- 通过SSH将MySQL绑定到本地端口——在控制台工作,而不是通过Mac OS
- yii2的AR模型对id自动去重的问题