asp.net mvc web api 参数输入多个参数
部分代码
apicontrol中
public class StudentController : ApiController
{
public HttpResponseMessage PostStudentsByReq(StudentReq studentReq, string criteria)
{
var students = studentRepository.GetAll().Where(
s => string.Equals(s.age.ToString(), studentReq.age.ToString(), StringComparison.OrdinalIgnoreCase));
var response = Request.CreateResponse(HttpStatusCode.OK, students);
return response;
//return students;
}
}
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 class StudentReq
{
public string name { get; set; }
public int id { get; set; }
public string gender { get; set; }
public int age { get; set; }
}
前台ajax jquery调用方法
function GetStudentByReq_Post() {
alert("开始");
var studentReq = {
name: 'ab',
id: '1',
gender: 'man',
age: '15'
};
var age = 22;
$.ajax({
url: 'api/student?criteria=full',
type: 'POST',
contentType: "application/json;charset=utf-8",
data: JSON.stringify(studentReq),
success: function (data) {
alert("aa");
// WriteResponse(data);
WriteResponses(data);
},
error: function (x, y, z) {
alert('The Student not found in the List for the given ID');
}
});
//Displays in a Table
function WriteResponses(students) {
var strResult = "<table><th>Name</th><th>Student ID</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 += "</table>";
$("#divResult").html(strResult);
}
}
html部分代码
<div id="divResult" style="margin-left: 15px"></div>
<div>
<button id="getStudentByReq2" onclick="GetStudentByReq_Post()">获取列表</button>
</div>