zoukankan      html  css  js  c++  java
  • 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>
  • 相关阅读:
    模拟出栈
    全排列 next_permutation 用法
    区间覆盖
    BFS GPLT L2-016 愿天下有情人都是失散多年的兄妹
    GPLT L2-014 列车调度
    图的联通分量个数统计(判断图是否联通)
    堆排序 GPLT L2-012 关于堆的判断
    牛客挑战赛 30 A 小G数数
    由树的中后序遍历求树的前层序遍历
    【HDOJ4699】Editor(对顶栈,模拟)
  • 原文地址:https://www.cnblogs.com/z_lb/p/2792188.html
Copyright © 2011-2022 走看看