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>

转载于:https://www.cnblogs.com/z_lb/archive/2012/11/28/2792188.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值