关于mvc ajax (post提交)——页面传值以及后台接收
// 前段页面,点击按钮触发Success事件
function success() {
var BusiName =“公司名称”;
var UserName = “用户”;
var UserPhone =“电话”;
var Email= “邮箱”;
var QQ = “QQ”;
var Address = “地址”;
var Remark = “备注(注释)”;
$.ajax({
url: "/Product/Defrayal",
type: "POST",
dataType: "json",
data: { "BusiName":"" + BusiName + "","UserName":"" + UserName + "","UserPhone":"" + UserPhone + "","Email":"" + Email + "","QQ":"" + QQ + "","Address":"" + Address + "","Remark":"" + Remark + ""},
success: function (data) {
alert(data);
},
error: function () {
alert("失败,请重试");
}
});
}
// 后台页面——用FormCollection 来接收数据;
[HttpPost]
public ActionResult Defrayal(FormCollection data)
{
//获取前段传过来的参数
bool bflag;
UserInfo ui = new UserInfo();
ui.BusiName = data["BusiName"].ToString();
ui.UserName = data["UserName"].ToString();
ui.UserPhone = data["UserPhone"].ToString();
ui.Email = data["Email"].ToString();
ui.QQ = data["QQ"].ToString();
ui.Address = data["Address"].ToString();
ui.Remark = data["Remark"].ToString();
if (true == bflag)
{
ViewBag.Success = "ok";
}
else
{
ViewBag.Success = "no";
}
// 返回json格式
return Json(ViewBag.Success);
}
// 后台页面——用Request.Form来接收数据;
[HttpPost]
public ActionResult Defrayal()
{
//获取前段传过来的参数
bool bflag;
UserInfo ui = new UserInfo();
ui.BusiName= Request.Form["BusiName"];
ui.UserName = Request.Form["UserName"];
ui.UserPhone = Request.Form["UserPhone"];
ui.Email = Request.Form["Email"];
if (true == bflag)
{
ViewBag.Success = "ok";
}
else
{
ViewBag.Success = "no";
}
// 返回json格式
return Json(ViewBag.Success);
}