形式恩codeD的Javascript INT []不拾起模型绑定 [英] Form Encoded Javascript int[] not Picked up By Model Binder
问题描述
昨天晚上,我试着放在一起的东西,我有自MVC2工作。
Last night I tried to put together something that I have had working since MVC2.
由于以下类:
public class RouteSaveViewModel
{
public string Title { get; set; }
public string Comments { get; set; }
public string DepartureDate { get; set; }
public string ArrivalDate { get; set; }
public List<int> LocationIds { get; set; }
}
...以下动作:
...and the action below:
[HttpPost]
public ViewResult SaveRoute(RouteSaveViewModel route)
{
// yada yada
return SomethingShiny();
}
我想在使用$阿贾克斯或.post的$的数据传递。形式是多步来收集用户数据并提交。在code我用它来构建的位置标识了是这样的:
I would like to pass in the data using a $.ajax or $.post. The form is multi-step to collect data from the user and submit it. The code I use to build the location Ids up is this:
var routeStopIds = [];
$(".route-stop").each(function(){
routeStopIds.push($(this).attr('data-id'));
});
我已经证实没有我预期的ID是这个数组中通过浏览器的开发工具和小提琴手。最后,提交数据,我映射出的对象,因为我在$ P $做过pvious MVC构建:
I have verified that there the ids I expect are in this array through in-browser dev tools and fiddler. Finally, to submit the data, I'm mapping out the object as I've done in previous MVC builds:
$.ajax({
type: 'post',
url: postUrl,
data: {
Title: $("#route-title").val(),
Comments: $("#route-description").val(),
DepartureDate: depDate,
ArrivalDate: arrDate,
LocationIds: routeStopIds
},
dataType: 'JSON'
});
我所看到的是,的所有的数据 - 除了 LocationIds
参数填充。我也试过,只是在 routeStopIds
阵列提交修改后的 SaveRoute
只接受清单&LT; INT&GT;
并没有出现被任何工作。无论哪种方式, LocationIds
为空,虽然有一个表单恩codeD在请求中的表单参数值。
What I'm seeing is that all data - except the LocationIds
param is populated. I have also tried to submit just the routeStopIds
array to a modified SaveRoute
that only accepts a List<int>
and that doesn't appear to be working either. Either way, LocationIds
is null, though there is a form-encoded value in the form parameters on the request.
{LocationIds%5b%5d=44&LocationIds%5b%5d=4&LocationIds%5b%5d=2}
...然而在HttpValueCollection就是 LocationIds []
,空数组。
所以,我失去了一些东西明显?也许不那么明显吗?我如何获取数组由模型绑定拾取?
So, am I missing something obvious? Maybe not-so-obvious? How do I get the array to be picked up by model binding?
推荐答案
记住那段,我说我有它,因为MVC2工作吗?啊,这是在这里:
高级模型绑定在那文章中,我明确提及以下内容:
Remember that part where I said I had it working since MVC2? Yeah, that was here: Advanced Model Binding In that post I explicitly mentioned the following:
注意:为了使阵列的结果与兼容
在ASP.NET MVC中绑定机制(截至MVC 2.0),我们需要使用
传统的设置在$阿贾克斯()。
Note: In order to make the results of the array compatible with the binding mechanism in ASP.NET MVC (as at MVC 2.0) we need to use the ‘traditional’ setting in $.ajax().
修改Ajax调用包含此设置被修复。所有是正确的雨水和柔软的羊毛小猫。或手套或什么的。
Modifying the ajax call to include this setting was the fix. All is right as rain and soft woolen kittens. Or mittens or whatever.
$.ajax({
type: 'post',
url: postUrl,
traditional: true, // <----- here be kittens
data: {
Title: $("#route-title").val(),
Comments: $("#route-description").val(),
DepartureDate: depDate,
ArrivalDate: arrDate,
LocationIds: routeStopIds
},
dataType: 'JSON'
});
这篇关于形式恩codeD的Javascript INT []不拾起模型绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!