我正在尝试通过我的ajax调用中的数据通过路由值发送一些ID并且我遇到了多个问题。
这就是我现在要做的事情......然后将其拆分为服务器上的数组。
@Url.Action("Save", "MyController", New With {.Ids = ViewData("Id1") & "," & ViewData("Id2") & "," & ViewData("Id3") & "," & ViewData("Id4")})");
这就是我想做的事情:
var url = "@Url.Action("Save", "MyController", New With {.Id1 = ViewData("Id1"), .Id2 = ViewData("Id2"), .Id3 = ViewData("Id3")})";
$.ajax({
type: "POST",
dataType: "json",
url: urlString,
data: json,
success: function(data) {
}
});
控制器方法签名:
<HttpPost()> _
Function Save(ByVal json As String, ByVal Id1 As String, ByVal Id2 As String, ByVal Id3 As String) As JsonResult
我也尝试使用类,而不是像这样(MyClass对我传入的参数有相应的属性名称):
<HttpPost()> _
Function Save(ByVal json As String, theClass as MyClass) As JsonResult
JSON数据如下所示:
"json={'control1' : 'test1', 'control2' : 'test2'}"
参数值:Id1是整数,Id2是guid,Id3是整数。
不幸的是,当我尝试这个时,我只会得到我的第一个参数,其余的都没有。
任何建议都会非常感激。
谢谢!
答案 0 :(得分:0)
您可以发送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) {
}
});
这将成功调用并将参数传递给以下操作:
<HttpPost()>
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) {
}
});
将绑定到以下操作:
<HttpPost()>
Public Function Create(ByVal ids As Integer(), ByVal foo As SomeViewModel) As ActionResult
...
End Function
但是如果你已经有了一个视图模型(顺便提一下),那么Ids可以直接成为你视图模型的属性,这样你的控制器动作现在就像这样:
<HttpPost()>
Public Function Create(ByVal foo As SomeViewModel) As ActionResult
...
End Function