[英]QueryString not Deserializing to Parameters (ASP.NET MVC)
我的控制器中有此功能
public ActionResult postcomment(string gCaptcha,string txtName, string txtEmail, string txtMessage)
我通过一个JQuery Ajax Querystring(PlainText)作为
gCaptcha=A&txtName=B&txtEmail=C&txtMessage=D
当我在注释后的代码上暂停时,参数的值为空白。 我需要做什么,RoutesConfig文件中有什么?
这是我的JQuery函数。
$.ajax({
type: "POST",
url: '@(Request.RawUrl + "/postcomment")',
dataType: "text",
data: data,
contentType: "text/plain; charset=utf-8",
success: function() {
alert('success');
},
failure: function(response) {
alert(response.d);
}
});
});
我也遇到过一个问题,值在帖子中为null。 我要做的是创建一个对象,并将该对象作为action方法上的参数。 因此,您将执行以下操作:
public class CommentViewModel
{
public string gCaptcha { get; set; }
public string txtName { get; set; }
public string txtEmail { get; set; }
public string txtMessage { get; set; }
}
public ActionResult postcomment(CommentViewModel comment)
正如Stephen在他的评论中提到的那样,使您的数据成为json对象,并从Ajax调用中删除contentType
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.