asp.net mvc--传值-前台->后台

时间:2022-11-28 18:56:21

前端传值->后端

一、Model Binding 方式

前台
@model ADMgr.Web.Models.ListModel
后台
[HttpPost]
public ActionResult Index(ListModel model) { }

二、表单提交方式

前台方式01
  <form id="formID" method="post" action="../Manage/ForgetPassword" enctype="multipart/form-data" class="form-inline">
        @Html.ActionLink("下载模版", "DownFileResult", null, new { @class = "btn btn-info " })
        <div class="input-append">
            <input type="file" class="input-prepend" name="file"/>
            <input type="submit" id="btn" class="btn btn-info" value="上传"/>
        </div>
    </form>
前台方式02
                     <div class="form-horizontal">
                                <div class="control-group ">
                                    <label class="control-label span2">用户名</label>
                                    <div class="control span2">
                                        <input type="text" id="adusername" name="adusername" />
                                    </div>
                                </div>
                                <div class="control-group ">
                                    <label class="control-label span2">手机号</label>
                                    <div class="control span2">
                                        <input type="text" id="mobile" name="mobile"/>
                                    </div>
                                </div>
                                    <input type="button" value="提交" class="btn btn-info pull-right" id="sumb"/>
                                </div>
                      </div>
 <script type="text/javascript">
        $("#sumb").click(function () {
            var obj = new Object();
            var name = $("#adusername").val();
            var mobile = $("#mobile").val();
            obj.name = name;
            obj.mobile = mobile;
            $.ajax({
                //提交数据的类型 POST GET
                type:"POST",
                //提交的网址
                url: "/Account/ForgetPassword",
                //提交的数据
                data:JSON.stringify(obj),
                //返回数据的格式
                datatype: "json",//"xml", "html", "script", "json", "jsonp", "text".
                //成功返回之后调用的函数
                success: function (data) {
                    var ob = JSON.parse(data);
                    if (ob == "1") {
                        toastr.success("重置密码成功");
                    } else if (ob == "3") {
                        toastr.error("一天内重置密码不能超过2次")
                    }else {
                        toastr.error("重置密码失败");
                    }

                }   ,
                //调用出错执行的函数
                error: function(){
                    toastr.info("请填写工号和手机号");
                }
            });

        });
后台接收01
        [AllowAnonymous]
        [HttpPost]
        public ActionResult ForgetPassword(FormCollection formCollection){
                    string ouCompany = formCollection["companies"];
                    //部门
                    string ouDepartment = formCollection["depts"];
                    // 职务
                    string title = formCollection["titles"];
                    //工号
                    string givenName = model.UIViewModel.Number;
        }
后台接收02
            var obj = Request.Form[0];
            Dictionary<string, string> dic = JsonConvert.DeserializeObject<Dictionary<string, string>>(obj);
            string name = dic["name"].ToString();
            string mobile = dic["mobile"].ToString();
后台接收03
             UserRole userRole = JsonConvert.DeserializeObject<UserRole>(json);