表单传值
通过页面的form表单向后台传参,action中写后台接口,表单提交方式必须是submit,参数信息会把input框name属性和value属性按key/value的形式传到后台指定地址。
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<from method="get">
<input type="text" name="username"/>
<input type="submit" name="提交" />
</from>
public class TestController : Controller
{
[HttpGet] //查询
//[HttpPost] //增删改
// GET: Test
//传值方式
public ActionResult Index()
{
//Request.QueryString[""]; Request.From [""]
//同名参数
//对象的同名属性 student username
return View();
}
//public ActionResult Index(string username)
//{
// return View();
//}
}
提交方式可以是get,也可以是post,区别在于传递的参数会不会在URL地址显示,不论get方式还是post方式,参数都会传到后台的。
<form id="formTest" action="user/userInfo.do" method="get">
<input type="text" name="name" value="zhangsan" />
<input type="text" name="sex" value="male" />
<input type="submit" value="提交" />
</form>
ajax传值
数据多时可以构建json对象,转换成json格式的String后传给后台
window.onload = function(){
var jsonData = {
name:"zhaosi",
sex:"male",
age:34,
address:"北京市海淀区",
phone:18801239876
}
var jsonData1 = JSON.stringify(jsonData); //格式转换
//传递到后台
$.ajax({
type:'POST',
data:jsonData1, //数据源
dataType:'json',
url:'user/UserInfo.do', //接口
success:function(data){ //成功传参后的回调函数
alert("发送成功");
alert(jsonData1);
},
error:function(e){ //传参失败的回调函数
alert("发送失败");
}
});
}