asp.net mvc jquery json 传递复杂参数
2012-11-28 17:46
661 查看
js中区分大小写;
model类
Controller
html页
add方法js方法jquery
html部分代码
前台jquery解析复杂对象对象中包含对象集合js代码
Html部分代码
model类
publicclassStudent
{
publicstringname{get;set;}
publicintid{get;set;}
publicstringgender{get;set;}
publicintage{get;set;}
publicList<Play>ListPlay{get;set;}
}
publicclassPlay
{
publicstringID{get;set;}
publicstringName{get;set;}
}
Controller
publicHttpResponseMessagePostStudent(Studentstudent)
{
student=studentRepository.Add(student);
varresponse=Request.CreateResponse<Student>(HttpStatusCode.Created,student);
stringuri=Url.Link("DefaultApi",new{id=student.id});
response.Headers.Location=newUri(uri);
returnresponse;
}
//[HttpPost]HttpResponseMessageIEnumerable<Student>
//[HttpPost]
//返回复杂对象
publicHttpResponseMessagePostStudentsByReq(StudentReqstudentReq,stringcriteria)
{
varstudents=studentRepository.GetAll().Where(
s=>string.Equals(s.age.ToString(),studentReq.age.ToString(),StringComparison.OrdinalIgnoreCase));
Studentstu=newStudent();
stu.age=1;
stu.gender="aa";
stu.id=112;
stu.name="aaa";
stu.ListPlay=newList<Play>();
Playplay=newPlay();
play.ID="11";
play.Name="张三";
stu.ListPlay.Add(play);
play=newPlay();
play.ID="12";
play.Name="name2";
stu.ListPlay.Add(play);
List<Student>list=newList<Student>();
list.Add(stu);
varresponse=Request.CreateResponse(HttpStatusCode.OK,list);
returnresponse;
//returnstudents;
}
html页
add方法js方法jquery
//AddsaStudenttotheList
functionAddStudent(){
debugger;
varPlay={
ID:'1',
Name:"张三"
};
varPlay2={
ID:'2',
Name:"李四"
};
varPlayList=newArray();
PlayList.push(Play);
PlayList.push(Play2);varstudent={
name:document.getElementById('newStudentName').value,
id:document.getElementById('newStudentId').value,
gender:document.getElementById('newStudentGender').value,
age:document.getElementById('newStudentAge').value,
ListPlay:PlayList
};
$.ajax({
url:'http://localhost:60792/api/student/',
type:'POST',
data:JSON.stringify(student),
contentType:"application/json;charset=utf-8",
success:function(data){
alert('StudentaddedSuccessfully');
GetAllStudents();
},
error:function(){
alert('StudentnotAdded');
}
});
}
html部分代码
<div>
<h1style="color:#f00">AddorUpdateaStudent</h1>
<tablestyle="margin-left:15px">
<tr>
<td><span>Name</span></td>
<td>
<inputid="newStudentName"type="text"/></td>
</tr>
<tr>
<td><span>ID</span></td>
<td>
<inputid="newStudentId"type="number"/></td>
</tr>
<tr>
<td><span>Gender</span></td>
<td>
<selectid="newStudentGender">
<optionvalue="Male">Male</option>
<optionvalue="Female">Female</option>
</select></td>
</tr>
<tr>
<td><span>Age</span></td>
<td>
<inputid="newStudentAge"type="number"/></td>
</tr>
<tr>
<td>
<buttonid="postStudent"onclick="AddStudent()">AddNewStudent</button></td>
<td>
<buttonid="putStudent"onclick="PutStudent()">UpdateStudent</button></td>
</tr>
</table>
</div>
前台jquery解析复杂对象对象中包含对象集合js代码
functionGetStudentByReq_Post(){
alert("开始");
debugger;
varstudentReq={
name:'ab',
id:'1',
gender:'man',
age:'15'
};
varage=22;
//jQuery.support.cors=true;
$.ajax({
url:'api/student?criteria=full',
type:'POST',
contentType:"application/json;charset=utf-8",
data:JSON.stringify(studentReq),
success:function(data){
alert(data);
//WriteResponse(data);
WriteResponses(data);
},
error:function(x,y,z){
alert('TheStudentnotfoundintheListforthegivenID');
}
});
//DisplaysinaTable
functionWriteResponses(students){
varstrResult="<table><th>Name</th><th>StudentID</th><th>Gender</th><th>Age</th>";
$.each(students,function(index,student){
strResult+="<tr><td>"+student.name+"</td><td>"+student.id+"</td><td>"+student.gender+"</td><td>"+student.age+"</td></tr>";
strResult+="<ul>";
$.each(student.ListPlay,function(index1,play){
strResult+="<li>"+play.ID+""+play.Name+"</li>"
});
strResult+="</ul>";
});
strResult+="</table>";
$("#divResult").html(strResult);
}
}
Html部分代码
<div>
<buttonid="getStudentByReq2"onclick="GetStudentByReq_Post()">获取列表2</button>
</div>
<divid="divResult"style="margin-left:15px"></div>
相关文章推荐
- 如何使用jQuery向asp.net Mvc传递复杂json数据-ModelBinder篇
- 如何使用jQuery向asp.net Mvc传递复杂json数据-Filter篇
- 使用jQuery向asp.net Mvc传递复杂json数据-ModelBinder篇
- 使用jQuery向asp.net Mvc传递复杂json数据-ModelBinder篇
- 如何使用jQuery向asp.net Mvc传递复杂json数据
- 使用jQuery向asp.net Mvc传递复杂json数据-ModelBinder篇
- 如何使用jQuery向asp.net Mvc传递复杂json数据
- 使用jQuery向asp.net Mvc传递复杂json数据-ModelBinder篇
- 如何使用jQuery向asp.net Mvc传递复杂json数据
- asp.net MVC接收前端传递的复杂json对象的简单处理方法
- asp.net jquery getjson 传递中文参数乱码问题
- asp.net mvc框架中,ajax传递json对象,并自动转换成参数对象的方法
- Jquery下Json数据的传递与解析(asp.net mvc与asp.net api下后台json接收方式的不同)
- Jquery下Json数据的传递与解析(asp.net mvc与asp.net api下后台json接收方式的不同)
- Jquery下Json数据的传递与解析(asp.net mvc与asp.net api下后台json接收方式的不同)
- asp.net jquery getjson 传递中文参数乱码问题
- asp.net mvc框架中,ajax传递json对象,并自动转换成参数对象的方法
- asp.net mvc框架中,ajax传递json对象,并自动转换成参数对象的方法
- ASP.NET MVC 2.0 与jQuery中的的JSON数据交互
- Asp.Net MVC使用HtmlHelper渲染,并传递FormCollection参数的陷阱