.net mvc 如果前后台传递的参数类型不同,那么会出错
例如前台这样传参数:
var postData = {}
postData.user_acc = "1;2";
$.post('/Test1/PostTest', postData, function (data) {
console.info(data);
})
后台:(使用int来接收)
[HttpPost]
public ActionResult PostTest(int user_acc)
{
return Json(new
{
Success = false,
Message = user_acc
});
}
这样写的结果是:系统无法直接将"1;2"转化为int型,会出错,如果传“12”,但系统会自动将字符串string类型12转化为int型12。
解决方法:可以在后台controller方法中再接收一个string类型的参数,两者不会冲突,这时如果传“12”,则int和string两个参数都能收到对应的值