您的位置:首页 > Web前端 > JQuery

asp.net mvc jquery json 传递复杂参数

2012-11-28 17:46 661 查看
js中区分大小写;

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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: