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>
  • 相关阅读:
    1-27 awk 基本使用
    计算机网络(一)带宽
    CRC检错技术原理
    Wireshark漫谈(一)
    SQL字符串拼接
    MySQL学习笔记(二)
    MySQL学习笔记(一)
    Java 反射机制(二)
    Java 反射机制(一)
    Windows使用MySQL数据库管理系统中文乱码问题
  • 原文地址:https://www.cnblogs.com/z_lb/p/2792188.html
Copyright © 2011-2022 走看看