c# mvc ajax 提交与 接收参数

1、ajax 请求

$.post()是jquery一个简单的 POST 请求功能以取代复杂 $.ajax .
参数:

url,[data],[callback],[type]
url:发送请求地址。
data:待发送 Key/value 参数。
callback:发送成功时回调函数。
type:返回内容格式,xml, html, script, json, text, _default。

model: data.field   这里  model为后台参数名  data.field 为实体对应 可以是 List

            $.post("/Admin/User/UserAdd", {model: data.field }, function (data) {
                alert(JSON.stringify(data));
            });

$.ajax jq的标准ajax请求   data 就是一个json对象

注意 contentType 为 application/x-www-form-urlencoded; charset=UTF-8   或者  application/json;charset=utf-8     否则后台接收不到 具体用哪个自己试吧

 $.ajax({
                type: "post",
                url: "/Admin/User/UserAdd",
                dataType: "json",
                data: data.field,
                contentType: 'application/json;charset=utf-8',//向后台传送格式
                success: function (data) {
                    if (data.success) {
                        $("searchResult").html(data.msg);
                    } else {
                        $("#searchResult").html("出现错误:" + data.msg);
                    }
                },
                error: function (jqXHR) {
                    aler("发生错误:" + jqXHR.status);
                }
            });

  

C# MVC 后台接收 

方法一:通过Request.Form

        [HttpPost]
        public ActionResult Test()
        {
            string id=Request.Form["id"];

            return View();
        }

方法二:通过映射到控制器方法参数

        [HttpPost]
        public ActionResult Test(string id)
        {
            //id是获取来自View表单POST过来的控件名为id的值

            return View();
        }

 方法三:通过映射到视图数据对象

     [HttpPost]
        public ActionResult Test(TModel model)
        {
            string id = model.id;

            return View();
        }
        [HttpPost]
        public ActionResult Test(List<TModel> model)
        {
            string id = model.id;

            return View();
        }

  

原文地址:https://www.cnblogs.com/su-king/p/11320261.html