MVC從視圖傳參到Controller幾種方式

來源:互聯網
上載者:User

標籤:des   style   blog   http   java   color   

簡單數組傳遞

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="001"/></p>    <p><input type="text" name="Name" value="Tom" /></p>    <p><input type="text" name="Age" value="24"/></p>    <p><input type="checkbox" name="Courses" value="語文" />        <input type="checkbox" name="Courses" value="數學" />        <input type="checkbox" name="Courses" value="外語" />    </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個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.