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

簡單數組傳遞html

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;
   }

單個模型傳遞ajax

@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;
        }

多個模型傳遞json

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)mvc

須要藉助ModelBinder來處理,添加一個類 :JsonModelBinderAttribute.csapp

 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.htmlide

相關文章
相關標籤/搜索