在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
js中区分大小写; model类 public class Student { public string name { get; set; } public int id { get; set; } public string gender { get; set; } public int age { get; set; } public List<Play> ListPlay { get; set; } }
public class Play { public string ID { get; set; } public string Name { get; set; } }
Controller public HttpResponseMessage PostStudent(Student student) {
student = studentRepository.Add(student);
var response = Request.CreateResponse<Student>(HttpStatusCode.Created, student); string uri = Url.Link("DefaultApi", new { id = student.id }); response.Headers.Location = new Uri(uri); return response; }
// [HttpPost] HttpResponseMessage IEnumerable<Student> // [HttpPost]
//返回复杂对象
public HttpResponseMessage PostStudentsByReq(StudentReq studentReq, string criteria) {
var students= studentRepository.GetAll().Where( s => string.Equals(s.age.ToString(), studentReq.age.ToString(), StringComparison.OrdinalIgnoreCase)); Student stu = new Student(); stu.age = 1;
stu.gender = "aa"; stu.id = 112;
stu.name = "aaa"; stu.ListPlay = new List<Play>(); Play play = new Play(); play.ID = "11"; play.Name = "张三"; stu.ListPlay.Add(play);
play = new Play(); play.ID = "12"; play.Name = "name2"; stu.ListPlay.Add(play);
List<Student> list = new List<Student>(); list.Add(stu);
var response = Request.CreateResponse(HttpStatusCode.OK, list); return response; //return students; }
html页 add方法js方法 jquery //Adds a Student to the List function AddStudent() { debugger; var Play = { ID: '1', Name: "张三" }; var Play2 = { ID: '2', Name: "李四" }; var PlayList = new Array(); PlayList.push(Play);
PlayList.push(Play2);
var student = { 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('Student added Successfully'); GetAllStudents();
},
error: function () { alert('Student not Added'); }
});
}
html部分代码 <div> <h1 style="color: #f00">Add or Update a Student </h1> <table style="margin-left: 15px"> <tr> <td><span>Name</span></td> <td> <input id="newStudentName" type="text" /></td> </tr> <tr> <td><span>ID</span></td> <td> <input id="newStudentId" type="number" /></td> </tr> <tr> <td><span>Gender</span></td> <td> <select id="newStudentGender"> <option value="Male">Male</option> <option value="Female">Female</option> </select></td> </tr> <tr> <td><span>Age</span></td> <td> <input id="newStudentAge" type="number" /></td> </tr> <tr> <td> <button id="postStudent" onclick="AddStudent()">Add New Student</button></td> <td> <button id="putStudent" onclick="PutStudent()">Update Student</button></td> 全部评论
专题导读
热门推荐
热门话题
阅读排行榜
|
请发表评论