随笔 - 435  文章 - 0  评论 - 111  阅读 - 62万 

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 接受参数 

1
2
3
4
5
6
7
[HttpPost]
public ActionResult Post(FormCollection form)
{
    var json = new { status = 0 };
    return Json(json);
 
}

  

posted on   Gu  阅读(976)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 什么是nginx的强缓存和协商缓存
· 一文读懂知识蒸馏
· Manus爆火,是硬核还是营销?
点击右上角即可分享
微信分享提示