C# MVC提交表单的四种方式(转)
一,MVC HtmlHelper方法
Html.BeginForm(actionName,controllerName,method,htmlAttributes){}
BeginRouteForm 方法 (HtmlHelper, String, Object, FormMethod)
二,传统Form表单Aciton属性提交
三,Jquery+Ajax 提交表单
四,MVC Controller控制器和表单参数传递
MVC HtmlHelper方法
一,Html.BeginForm(actionName,controllerName,method,htmlAttributes){}
注:所有要提交的内容包括按钮都必须在{ }内
参数
- htmlHelper
- 类型:System.Web.Mvc.HtmlHelper
此方法扩展的 HTML 帮助器实例。
- actionName
- 类型:System.String
操作方法的名称。
- controllerName
- 类型:System.String
控制器的名称。
- routeValues
- 类型:System.Object
一个包含路由参数的对象。 通过检查对象的属性,利用反射检索参数。 此对象通常是使用对象初始值设定项语法创建的。
- method
- 类型:System.Web.Mvc.FormMethod
用于处理窗体的 HTTP 方法(GET 或 POST)。
- htmlAttributes
- 类型:System.Object
一个对象,其中包含要为该元素设置的 HTML 特性。
返回值
类型:System.Web.Mvc.Html.MvcForm
<form> 开始标记。
用法说明
在 Visual Basic 和 C# 中,您可以对 HtmlHelper 类型的任何对象将此方法作为实例方法调用。在您使用实例方法语法调用此方法时,将忽略第一个参数
Html.BeginForm 方法示例
MVC View代码

<h1>在线申请</h1>
@using (Html.BeginForm("Apply", "Star", FormMethod.Post, new {@class="MyForm"}))
{
<div class="application_b_3">
<table width="820" border="0">
<tr>
<td width="80" height="50">达人类型</td>
<td width="730">
@Html.DropDownListFor(m => m.StarModel.TypeID, Model.DropList, new { id = "type", @class = "my-" })
</td>
</tr>
<tr>
<td height="50">首页达人照</td>
<td>
<div class="picture_an" id="UploadPhoto" style="width: 142px">
<a href="javascript:void(0);" class="btn_addPic"><span><em>+</em>上传照片</span>
<input tabindex="3" title="支持jpg、jpeg、gif、png格式,文件小于5M" size="3" name="pic" id="absFileInput" class="filePrew" type="file" />
</a>
</div>
</td>
</tr>
<tr>
<td height="50"></td>
<td>
@Html.HiddenFor(m => m.StarModel.UserGravatar, new { id = "SXtPhoto" })
<img src="" id="imgPhoto" height="176px" />
</td>
</tr>
<tr>
<td height="100">自荐理由</td>
<td>
@Html.TextAreaFor(m => m.StarModel.ApplyReason, new { id = "tDesc" })
</td>
</tr>
<tr>
<td height="50"></td>
<td>
<a href=" javascript:void(0)" id="btnApplication"><img src="@Url.Content("~/Areas/SNS/Themes/Default/Content/images/ap_9.gif")" alt="" /></a>
</td>
</tr>
</table>
</div>
}

二,BeginRouteForm 方法 (HtmlHelper, String, Object, FormMethod)
参数
- htmlHelper
- 类型:System.Web.Mvc.HtmlHelper
此方法扩展的 HTML 帮助程序实例。
- routeName
- 类型:System.String
用于获取窗体发布 URL 的路由的名称。
- routeValues
- 类型:System.Object
一个包含路由参数的对象。 通过检查对象的属性,利用反射检索参数。 此对象通常是使用对象初始值设定项语法创建的。
- method
- 类型:System.Web.Mvc.FormMethod
用于处理窗体的 HTTP 方法(GET 或 POST)。
返回值
类型:System.Web.Mvc.Html.MvcForm
一个开始 <form> 标记。
使用说明
在 Visual Basic 和 C# 中,可以在 HtmlHelper 类型的任何对象上将此方法作为实例方法来调用。当使用实例方法语法调用此方法时,请省略第一个参数。
BeginRouteForm示例

<div class="group-search-box clearfix">
@using (Html.BeginRouteForm("SearchPage", new { cityID = Model.CityID, productType = Model.CurrentProductType, currentPageIndex = Model.CurrentIndex, keyword = Model.keyword }, FormMethod.Get))
{
<input type="text" name="keyword" class="search-ipt" value=@Model.keyword>
<input type="submit" id="submit" value="搜 索" class="gsearch-btn" >
}
</div>

传统Form表单Aciton属性提交
直接利用html表单的Aciton属性进行提交。
方法示例

<form id="askform" action="@Url.Action("AskForm")" method="post">
<div class="title-area-outter clearfix">
<span></span>
<select id="dplBDTType" name="dplBDTType"></select>
<select id="selType" name="selType"></select>
</div>
</form>

Jquery+Ajax 提交表单
方法示例
View 部分
<div class="issue" id="postWeibo" style="width: 80px">
<a href="javascript:void(0)" class="publish-btn">发布</a>
</div>
Jquery和Ajax部分

//发布长微博
$("#postWeibo").click(function () {
var blogID = $("#hfID").val();
var title = $("#title").val();
var imgurl = $("#previewImgHide").val();
var des = editor.getContent();
if (title == "") {
ShowFailTip('微博标题不能为空!');
return;
} if (title.length >= 40) {
ShowFailTip("微博标题不能超出40个字!");
return;
}
//检查是否数字
if (isNaN(fee)) {
ShowFailTip("不能包含文本必须是数值!");
return;
}
if (ContainsDisWords(title + des)) {
ShowFailTip('您输入的内容含有禁用词,请重新输入!');
return;
} $.ajax({
url: "/fx" + $Maticsoft.BasePath + "Blog/AjaxUpdate",
type: 'POST',
async: false,
dataType: 'html',
// timeout: 10000,
data: { Title: title, CityID: city, Fee: fee, CategoryID: category, Days: days, Tag: tag, startDate: startdate, endDate: enddate, ImgUrl: imgurl, Des: des, BlogID: blogID }, //
success: function (resultData) { $(".dialogDiv").hide();
if (resultData == "No") {
ShowFailTip("操作失败,请您重试!");
} else if (resultData == "AA") {
$.jBox.tip('管理员不能操作', 'error');
} else {
var data = $(resultData); }
}
});
});

MVC Controller控制器和表单参数传递
1.普通参数
HTML标签name 和参数名一样。
public ActionResult AskForm(string txtTitle, string txtEditor, string dplBDTType, string selType, string txtYZM)
{
}
2.实体传参
HTML标签name 属性和Model属性保持一致
[HttpPost]
public ActionResult Apply(ViewModel.SNS.Star model)
{
//逻辑
}
4.表单集合传参
[HttpPost]
public ActionResult Apply(FormCollection Form)
{
//逻辑
}
C# MVC提交表单的四种方式(转)的更多相关文章
- 【总结-前台发送后台接收表单】MVC提交表单的四种方式
https://www.cnblogs.com/chenwolong/p/Form.html#commentform 后台控制器接收前台表单参数三种方法: 一.普通参数 HTML标签name 和参数名 ...
- Mvc 提交表单的4种方法全程详解
一,MVC HtmlHelper方法 Html.BeginForm(actionName,controllerName,method,htmlAttributes){} BeginRouteForm ...
- Mvc 提交表单的4种方法
一,MVC HtmlHelper方法 1. Html.BeginForm(actionName,controllerName,method,htmlAttributes){} 2. ...
- MVC中提交表单的4种方式
一,MVC HtmlHelper方法 Html.BeginForm(actionName,controllerName,method,htmlAttributes){} BeginRouteForm ...
- 使用JQuery提交表单的两种方式选择
有一个表单,如果使用JQuery提交的话,可以使用下面2中方式,但他们的区别却是根据实际需求需要进行选择的. 第一种:表单按照action路径提交后,页面会刷新. $("#id") ...
- 使用AJAX异步提交表单的几种方式
方式一 手工收集所有的用户输入,封装为大的“k1=v1&k2=v2…”键值对形式,使用$.post(url, data,fn)把数据提交给服务器 $.ajax({ type:'post', u ...
- jQuery提交表单的几种方式
方式一: $.post('../Ajax/GoodsAjax.ashx?cmd=getGsList', function (result) { var result = eval('(' + re ...
- JavaScript—异步提交表单的6种方式
FormData的详细介绍及使用请点击此处,那里对FormData的方法和事件已经表述的非常清楚,这里就不再浪费时间在介绍一遍了.本文主要针对FormData对象的使用以及异步文件上传进行详细的说明. ...
- .net c# MVC提交表单的4种方法
https://blog.csdn.net/qingkaqingka/article/details/85047781
随机推荐
- 【转载】wireshark:no interface can be used for capturing in this system with the current configuration
转自:wireshark:no interface can be used for capturing in this system with the current configuration 通过 ...
- Windows下编译objective-C
Windows下编译objective-C 2011-08-31 14:32 630人阅读 评论(0) 收藏 举报 windowscocoa工具objective clibraryxcode 目录 ...
- linux下搭建SVN服务器完全手册【摘抄】
系统环境 RHEL5.4最小化安装(关iptables,关selinux) + ssh + yum 一,安装必须的软件包. yum install subversion ( ...
- kafka监控系统
Metrics-Java版的指标度量工具之一 Metrics-Java版的指标度量工具之二 JAVA Metrics 度量工具使用介绍1 JAVA Metrics度量工具 - Metrics Core ...
- IE 的resize事件问题
window的resize事件,真的让人无语! 我在动态设置元素的HTML内容后,窗口高度变化了,可是却不触发resize事件. 但是我在访问document.documentElement.scro ...
- FW:使用weave管理docker网络
Posted on 2014-11-12 22:20 feisky 阅读(1761) 评论(0) 编辑 收藏 weave简介 Weave creates a virtual network that ...
- go 安装
安装golang centos7 直接安装golang yum install golang centos6需添加软件源,然后再安装 rpm -ivh http://ftp.riken.jp/Linu ...
- [LeetCode]题解(python):081 - Search in Rotated Sorted Array II
题目来源 https://leetcode.com/problems/search-in-rotated-sorted-array-ii/ Follow up for "Search in ...
- linux 多个ssh key 登录远程主机
登录A主机使用分配的sshkey需要指定特定的key,ssh -i ~/.ssh/xxx_id_rsa_privatekey username@host , 指定key后登录提示: Agent ...
- The identity used to sign the executable is no longer valid.
昨天运行还好好的,今天Xcode突然报这个错误. 在网上搜索了一番,也没有找到合适的解决办法. 那怎么办呢? 于是我就登陆了Appstore的开发者账号,发现里面的证书都是invalid状态,我想应该 ...