C# MVC中的Ajax帖子。。。为什么字符串为空?
基本上,我在MVC应用程序中创建一个请求系统。我有这个createrequest部分,在这里我可以从Telerik的DropDownList中选择我想要执行的请求类型。我想做的是,每次我从列表中选择某个内容时,都会出现一个局部视图,其中包含与该类型请求相关的表单 这是我在Create.cshtml视图中的ajax帖子:C# MVC中的Ajax帖子。。。为什么字符串为空?,c#,asp.net,ajax,asp.net-mvc,C#,Asp.net,Ajax,Asp.net Mvc,基本上,我在MVC应用程序中创建一个请求系统。我有这个createrequest部分,在这里我可以从Telerik的DropDownList中选择我想要执行的请求类型。我想做的是,每次我从列表中选择某个内容时,都会出现一个局部视图,其中包含与该类型请求相关的表单 这是我在Create.cshtml视图中的ajax帖子: <script> function change() { var value = $("#RequestType").val();
<script>
function change() {
var value = $("#RequestType").val();
alert(value);
$.ajax({
url: "/Request/CreateRequestForm",
type: "get",
data: { requestValue : JSON.stringify(value)}
}).done(function (data) {
$("#partialplaceholder").html(data);
}).fail(function () {
alert('error');
})
};
</script>
每次偶数触发时,我的dropDownValue字符串都为空。。。为什么?提前谢谢!:
编辑
视图代码
<h1>Create New Request</h1>
@(Html.Kendo().DropDownList()
.Name("RequestType")
.DataTextField("Text")
.DataValueField("Value")
.Events(e => e.Change("change"))
.BindTo(new List<SelectListItem>() {
new SelectListItem() {
Text = "Absence",
Value = "1"
},
new SelectListItem() {
Text = "Additional Hours",
Value = "2"
},
new SelectListItem() {
Text = "Compensation Day",
Value = "3"
},
new SelectListItem() {
Text = "Error Correction",
Value = "4"
},
new SelectListItem() {
Text = "Vacation",
Value = "5"
}
})
.Value("1")
)
<script>
function change() {
var value = $("#RequestType").val();
alert(value);
$.ajax({
url: "/Request/CreateRequestForm",
type: "get",
data: { requestValue : JSON.stringify(value)}
}).done(function (data) {
$("#partialplaceholder").html(data);
}).fail(function () {
alert('error');
})
};
</script>
<div id="partialplaceholder">
</div>
首先:标题说你正在做一个post请求,但是在你的代码中有一个get请求 第二:为了使其工作,您必须更改要发送的javascript中的数据名称,以匹配c代码中的参数名称,如:
<script>
function change() {
var value = $("#RequestType").val();
alert(value);
$.ajax({
url: "/Request/CreateRequestForm",
type: "get",
data: { dropDownValue: JSON.stringify(value)}
}).done(function (data) {
$("#partialplaceholder").html(data);
}).fail(function () {
alert('error');
})
};
</script>
第三:我很确定您不需要JSON。将数据字符串化。有关Stringify方法和用法的更多详细信息,请检查发送的名称/值对是否为requestValue:someValue,但CreateRequestForm方法有一个名为dropDownValue而不是requestValue的参数-更改一个或另一个,使其匹配,并且不必对值进行Stringify,因为您显然需要一个int参数应为int requestValue而不是stringIt。。。谢谢:
<script>
function change() {
var value = $("#RequestType").val();
alert(value);
$.ajax({
url: "/Request/CreateRequestForm",
type: "get",
data: { dropDownValue: JSON.stringify(value)}
}).done(function (data) {
$("#partialplaceholder").html(data);
}).fail(function () {
alert('error');
})
};
</script>
[HttpGet]
public PartialViewResult CreateRequestForm(string requestValue )
{
...
}