您可以发送JSON请求:
var url = '@Url.Action("Create", "FormGeneration")';
var jsonData = @Html.Raw(Json.Encode(New With { .Id1 = ViewData("Id1"), .Id2 = ViewData("Id2"), .Id3 = ViewData("Id3") }));
// assign some other properties
jsonData.foo = 'bar';
$.ajax({
url: url,
type: 'POST',
contentType: 'application/json',
data: JSON.stringify(jsonData),
success: function(data) {
}
});
这将成功调用并将参数传递给以下操作:
Public Function Create(ByVal id1 As Integer, ByVal id2 As Integer, ByVal id3 As Integer, ByVal foo As SomeViewModel) As ActionResult
...
End Function
您也可以将这些整数作为数组发送:
var url = '@Url.Action("Create", "FormGeneration")';
var jsonData = @Html.Raw(Json.Encode(New With { .ids = { ViewData("Id1"), ViewData("Id2"), ViewData("Id3") } }));
// assign some other properties
jsonData.foo = 'bar';
$.ajax({
url: url,
type: 'POST',
contentType: 'application/json',
data: JSON.stringify(jsonData),
success: function(data) {
}
});
将绑定到以下操作:
Public Function Create(ByVal ids As Integer(), ByVal foo As SomeViewModel) As ActionResult
...
End Function
但是如果你已经有了一个视图模型(顺便提一下),那么Ids可以直接成为你视图模型的属性,这样你的控制器动作现在就像这样:
Public Function Create(ByVal foo As SomeViewModel) As ActionResult
...
End Function