zoukankan      html  css  js  c++  java
  • MVC的前端和后端的Model Binding

    1.前端提交JSON 字符串

    {"id":13,"title":"这里是标题33","day":"2018-8-16","status":0,"arr":[{"type":"r","quest":"333","q1":"3","q2":"3","q3":"3"},{"type":"c","quest":"444","q1":"4","q2":"4","q3":"4"},{"type":"t","quest":"5"}]}
                $.ajax({
                    url: "/survey/post",
                    method:"post",
                    data: json,
                    contentType: "application/json",
                    success: function (data) {
                        console.log(data.status);
                    }
                })  

    后端MVC controller ( version<=5) , 如果是WebAPI, 则要加上[FromBody]修饰参数

        public class SurveyDTO
        {
            public string id { get; set; }
            public string title { get; set; }
            public DateTime day { get; set; }
            public string status { get; set; }
            public SurveyItem[] arr { get; set; }
        }
        public class SurveyItem {
            public string type { get; set; }
            public string quest { get; set; }
            public string q1 { get; set; }
            public string q2 { get; set; }
            public string q3 { get; set; }
        }
            [HttpPost]
            public ActionResult Post(SurveyDTO data)
            {}

    JSON带有数组的话, 通常不能直接用EntityFramework的实体.要重新定义.

    2. 前端使用表单提交

    $.post('/survey/postForm', $('#form').serialize(), function (json) {
                if (json.isSuccess) {
                   
                }
    
            });

    $.ajax({
    url: "/survey/postForm",
    method: "post",
    data: "id=" + key + "&title=" + title + "&day=" + date + "&status=" + sta + "&arr=" + JSON.stringify(this.storageArr),
    success: function (data) {
    console.log(data.status);
    }
    })

     

    后端使用Formcollection 接受参数 

            [HttpPost]
            public ActionResult Post(FormCollection form)
            {
                var json = new { status = 0 };
                return Json(json);
    
            }
    

      

  • 相关阅读:
    数据库 Mysql事务详解
    数据库 Mysql内容补充二
    数据库 Mysql内容补充一
    优化Django ORM中的性能问题(含prefetch_related 和 select_related)
    django高级
    百度,谷歌,360,搜狗,神马等蜘蛛IP段
    中国电信、联通、移动、教育IP分布
    sed 给文件每行末尾追加相同字符
    centos7 lvs keepalived做DNS集群负载
    Notepad++ 删除空白行的方法
  • 原文地址:https://www.cnblogs.com/zitjubiz/p/9550130.html
Copyright © 2011-2022 走看看