asp.net web mvc form表单提交
1、指定action
<form action="/control/action1" method="post"> <input type="submit" value="保存" /> </form>
public string action1(FormCollection form){}//返回string或JsonResult等,通过form["xxx"]取页面表单对应name="xxx"的值
2、@using,如果上传文件可以在FormMethod.Post后加上new { enctype = "multipart/form-data" }(加其他参数new {aaa="xxx"},未测试)
@using (Html.BeginForm("action1", "control", FormMethod.Post)) { <input type="submit" value="提交" /> }
3、ajax
$(':submit').on('click', function () { $.ajax({ url: "/home/BookmarkIntoFile", type: "POST", data: { str: JSON.stringify($('form').serializeObject()) },//(1)后台方法参数用string str接收 //data: JSON.stringify($('form').serializeObject()),//(2)用这个格式参数时,需要和下一行一起取消注释,后台方法参数用class aaa接收,aaa属性值对应页面相同name标签值 //contentType: "application/json", success: function (res) { } }); return false;//提交后不跳转刷新页面,或者不用<input type="submit">的方式,改为设置一个button按钮 }); //自动将form表单封装成json对象 $.fn.serializeObject = function () { var o = {}; var a = this.serializeArray(); $.each(a, function () { if (o[this.name]) { if (!o[this.name].push) { o[this.name] = [o[this.name]]; } o[this.name].push(this.value || ''); } else { o[this.name] = this.value || ''; } }); return o; };
参考:
http://www.360doc.com/content/14/0805/09/10504424_399511801.shtml