• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

asp.netmvcjqueryjson传递复杂参数

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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>
 
                       
                    
                    

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
ASP.Net核心对象HttpRequest发布时间:2022-07-10
下一篇:
ASP.NETMVC的WebApi的实练发布时间:2022-07-10
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap