首页 > 代码库 > MVC从视图传参到Controller几种方式
MVC从视图传参到Controller几种方式
简单数组传递
var array = ["aaa", "bbb", "ccc"]; $.ajax({ url:"@Url.Action("Test")", type: "POST", data: { array: array }, traditional: true //需要写上该属性,Controller才能接收到 }); public ActionResult Test(List<string> array) { return null; }
单个模型传递
@using (Html.BeginForm("Test", "Home")) { <p><input type="text" name="No" value=http://www.mamicode.com/"001"/></p> <p><input type="text" name="Name" value=http://www.mamicode.com/"Tom" /></p> <p><input type="text" name="Age" value=http://www.mamicode.com/"24"/></p> <p><input type="checkbox" name="Courses" value=http://www.mamicode.com/"语文" /> <input type="checkbox" name="Courses" value=http://www.mamicode.com/"数学" /> <input type="checkbox" name="Courses" value=http://www.mamicode.com/"外语" /> </p> <p><button type="submit">提交</button></p>}public ActionResult Test(Student student) { return null; }
多个模型传递
1.方式一
var models = []; models.push({ No: "001", Name: "Tom", Age: 20, Courses: ["语文", "数学", "外语"] }); models.push({ No: "002", Name: "Jeff", Age: 21, Courses: ["语文", "数学", "外语"] }); models.push({ No: "003", Name: "Hacks", Age: 22, Courses: ["语文", "数学", "外语"] }); $.ajax({ url: ‘@Url.Action("Test")‘, data: JSON.stringify(models),//第一个地方,需要进行JSON序列化 type: ‘POST‘, contentType: ‘application/json‘,//第二个地方,需要声明为‘application/json‘,默认‘application/x-www-form-urlencoded‘ success: function (data) { } });public ActionResult Test(List<Student> models) { return null; }
2.方式二 (Model Binder)
需要借助ModelBinder来处理,添加一个类 :JsonModelBinderAttribute.cs
public class JsonModelBinderAttribute : CustomModelBinderAttribute { public override IModelBinder GetBinder() { return new JsonBinder(); } } public class JsonBinder : IModelBinder { public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { //return base.BindModel(controllerContext, bindingContext); if (controllerContext == null) { throw new ArgumentNullException("controllerContext"); } if (bindingContext == null) { throw new ArgumentNullException("bindingContext"); } var prefix = bindingContext.ModelName; string jsonString = controllerContext.RequestContext.HttpContext.Request.Params[prefix]; if (jsonString != null) { var serializer = new JavaScriptSerializer(); var result = serializer.Deserialize(jsonString, bindingContext.ModelType); return result; } else { return null; } } }
var models = []; models.push({ No: "001", Name: "Tom", Age: 20, Courses: ["语文", "数学", "外语"] }); models.push({ No: "002", Name: "Jeff", Age: 21, Courses: ["语文", "数学", "外语"] }); models.push({ No: "003", Name: "Hacks", Age: 22, Courses: ["语文", "数学", "外语"] }); $.ajax({ url: ‘@Url.Action("Test")‘, data: { models: JSON.stringify(models) }, type: ‘POST‘, success: function (data) { } });public ActionResult Test([JsonModelBinder]List<Student> models) { return null; }
参考:http://ishwor.cyberbudsonline.com/2012/07/fun-with-aspnet-mvc-3-custom-json-model-binder.html
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。