[英]ASP.NET MVC 4 C# Failed Connected Ajax Post With Model
I'm making an AAX request using jQuery, post like this: 我正在使用jQuery发出AAX请求,像这样发布:
$("#frm").submit(function (e) {
$.ajax({
url: "/My/Controller/Action",
type: "POST",
data: $(this).serialize(),
dataType: "json",
success: function (status) {
alert("Success");
},
error: function () {
alert("Error");
}
});
e.preventDefault();
});
and here my controller : 这是我的控制器:
[AcceptVerbs(HttpVerbs.Post)]
public JsonResult QuizCalculation()
{
foreach (string keys in Response.Form.AllKeys)
{
var m = db.Quiz.Single(h => h.id == keys);
}
Request.AcceptTypes.Contains("application/json");
return Json(new { status = true });
}
But it always returns an error : 但是它总是返回一个错误:
Operator '==' cannot be applied to operands of type 'int' and 'string'
运算符'=='不能应用于类型'int'和'string'的操作数
in part => h.id == keys
部分
=> h.id == keys
Please help me. 请帮我。 Any idea on what is causing this?
知道是什么原因造成的吗?
You need to either convert id
to a string, or keys
to an int
. 您需要将
id
转换为字符串,或者将keys
转换为int
。 Try this: 尝试这个:
foreach (string keys in Response.Form.AllKeys)
{
var m = db.Quiz.Single(h => h.id.ToString() == keys);
}
Or this: 或这个:
foreach (string keys in Response.Form.AllKeys)
{
var m = db.Quiz.Single(h => h.id == Int16.Parse(keys));
}
Also, you should look in to using ModelBinding to receive your parameters, instead of looping through the Form
. 另外,您应该考虑使用ModelBinding接收参数,而不是遍历
Form
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.