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); }
MVC的前端和后端的Model Binding的更多相关文章
- Spring MVC之中前端向后端传数据
Spring MVC之中前端向后端传数据和后端向前端传数据是数据流动的两个方向, 在此先介绍前端向后端传数据的情况. 一般而言, 前端向后端传数据的场景, 大多是出现了表单的提交,form表单的内容在 ...
- [ASP.NET MVC 小牛之路]15 - Model Binding
Model Binding(模型绑定)是 MVC 框架根据 HTTP 请求数据创建 .NET 对象的一个过程.我们之前所有示例中传递给 Action 方法参数的对象都是在 Model Binding ...
- Asp.net MVC使用Model Binding解除Session, Cookie等依赖
上篇文章"Asp.net MVC使用Filter解除Session, Cookie等依赖"介绍了如何使用Filter来解除对于Session, Cookie的依赖.其实这个也可以通 ...
- [ASP.NET MVC] Model Binding With NameValueCollectionValueProvider
[ASP.NET MVC] Model Binding With NameValueCollectionValueProvider 范例下载 范例程序代码:点此下载 问题情景 一般Web网站,都是以H ...
- 【ASP.NET MVC 学习笔记】- 16 Model Binding(模型绑定)
本文参考:http://www.cnblogs.com/willick/p/3424188.html. 1.Model Binding是Http请求和Action方法之间的桥梁,是MVC框架根据Htt ...
- spring mvc日期转换(前端到后端,后端到前端)
在做web开发的时候,页面传入的都是String类型,SpringMVC可以对一些基本的类型进行转换,但是对于日期类的转换可能就需要我们配置. 1.如果查询类使我们自己写,那么在属性前面加上@Date ...
- 系统架构:Web应用架构的新趋势---前端和后端分离的一点想法
最近研究servlet,看书时候书里讲到了c/s架构到b/s架构的演变,讲servlet的书都很老了,现在的b/s架构已经不是几年前的b/s架构,其实b/s架构就是web应用开发,对于这样的架构我们现 ...
- ASP.NET MVC3 Dynamically added form fields model binding
Adding new Item to a list of items, inline is a very nice feature you can provide to your user. Thi ...
- [转载]Web前端和后端之区分,以及面临的挑战
原文地址:Web前端和后端之区分,以及面临的挑战[转]作者:joyostyle 在我们实际的开发过程中,我们当前这样定位前端.后端开发人员. 1)前端开发人员:精通JS,能熟练应用JQuery,懂CS ...
随机推荐
- 每日质量NPM包模态框_react-modal
一.react-modal 官方定义: Accessible modal dialog component for React.JS 理解: 一个容易使用的React模态框组件 二.用法 有时候我们不 ...
- React入门实例:组件化+react-redux实现网上商城(1)
项目运行 1.git clone https://github.com/soybeanxiaobi/React_demo_onlineShop 2.cd React_demo_onlineShop(文 ...
- 如何用git把本地代码上传到github
注册账户以及创建仓库 要想使用github第一步当然是注册github账号了.之后就可以创建仓库了(免费用户只能建公共仓库),Create a New Repository,填好名称后Create,之 ...
- STL_string.ZC
1.转成 小写/大写 #include <algorithm>using namespace std; // 转成小写transform(_strAttrNameG.begin(), _s ...
- 随机--相关(Fisher_Yates算法)
Fisher_Yates算法 void ShuffleArray_Fisher_Yates(char* arr, int len) { int i = len, j; char t ...
- MySQL学习(二)
1 增删改查是针对表来说的. 2 创建一个表 mysql> create table stu( -> id int primary key auto_increment, -> sn ...
- 将本地项目上传到gitlab下
转载自: https://blog.csdn.net/litianxiang_kaola/article/details/74075151 1.安装git https://git-scm.com ...
- CC2 条理分明-----AACTP教你谈恋爱
AACTP是一个神奇的地方,这里可以培养你的自信,培养你的沟通表达能力,培养你的领导管理能力:但是你不知道的是AACTP还可以培养你恋爱的本领.想知道是怎么回事吗?听我给你慢慢说来.我把恋爱分为三 ...
- Codeforces 1005 F - Berland and the Shortest Paths
F - Berland and the Shortest Paths 思路: bfs+dfs 首先,bfs找出1到其他点的最短路径大小dis[i] 然后对于2...n中的每个节点u,找到它所能改变的所 ...
- python读取配置文件&&简单封装
之前有做过把爬虫数据写到数据库中的练习,这次想把数据库信息抽离到一个ini配置文件中,这样做的好处在于可以在配置文件中添加多个数据库,方便切换(另外配置文件也可以添加诸如邮箱.url等信息) 1.co ...