Html.BeginForm与Ajax.BeginForm都是MVC架构中的表单元素,它们从字面上可以看到区别,即Html.BeginForm是普通的表单提交,而Ajax.BeginForm是支持异步的表单提交,这对于我们开发者来说是一个福音,我们不用再自己去用JQ代码了,直接用MVC自代的Ajax.BeginForm就可以很容易的完成一个异步的表单提交动作。

Html.BeginForm的原型解释:

一、

 @using (Html.BeginForm()) {} //提交到当前页面
@using (Html.BeginForm())
{
    <p>Your name:@Html.TextBoxFor(x=>x.Name)</p>
    <p>Your email:@Html.TextBoxFor(x => x.Email)</p>
    <p>Your phone:@Html.TextBoxFor(x => x.Phone)</p>
    <p>
    Will you attend?
    @Html.DropDownListFor(x => x.WillAttend, new[]{
    new SelectListItem(){
        Text="Yes.I'll be there",Value=bool.TrueString
    },new SelectListItem(){
        Text="No,I can't cpme",Value=bool.FalseString
    }
}, "Choose an option")
    </p>
    <input type="submit"  value="Submit RSVP"/>

}
 
二、
 @using (Html.BeginForm(new {} )) {} //提交到当前页面,并可以传递参数
@using (Html.BeginForm(new {Name="yhf2014" }))
{
   @* <p>Your name:@Html.TextBoxFor(x=>x.Name)</p>*@
    <p>Your email:@Html.TextBoxFor(x => x.Email)</p>
    <p>Your phone:@Html.TextBoxFor(x => x.Phone)</p>
    <p>
    Will you attend?
    @Html.DropDownListFor(x => x.WillAttend, new[]{
    new SelectListItem(){
        Text="Yes.I'll be there",Value=bool.TrueString
    },new SelectListItem(){
        Text="No,I can't cpme",Value=bool.FalseString
    }
}, "Choose an option")
    </p>
    <input type="submit"  value="Submit RSVP"/>

}三、
@using (Html.BeginForm("action","controller")) {} //提交到指定controller下的action中
@using (Html.BeginForm("RsvpForm", "Home"))
{
    <p>Your name:@Html.TextBoxFor(x=>x.Name)</p>
    <p>Your email:@Html.TextBoxFor(x => x.Email)</p>
    <p>Your phone:@Html.TextBoxFor(x => x.Phone)</p>
    <p>
    Will you attend?
    @Html.DropDownListFor(x => x.WillAttend, new[]{
    new SelectListItem(){
        Text="Yes.I'll be there",Value=bool.TrueString
    },new SelectListItem(){
        Text="No,I can't cpme",Value=bool.FalseString
    }
}, "Choose an option")
    </p>
    <input type="submit"  value="Submit RSVP"/>

}

四、

@using (Html.BeginForm("action","controller",FormMethod.POST)) {} //提交到指定controller下的action中,并指定提交方式
@using (Html.BeginForm("RsvpForm", "Home", FormMethod.Post))
{
    <p>Your name:@Html.TextBoxFor(x=>x.Name)</p>
    <p>Your email:@Html.TextBoxFor(x => x.Email)</p>
    <p>Your phone:@Html.TextBoxFor(x => x.Phone)</p>
    <p>
    Will you attend?
    @Html.DropDownListFor(x => x.WillAttend, new[]{
    new SelectListItem(){
        Text="Yes.I'll be there",Value=bool.TrueString
    },new SelectListItem(){
        Text="No,I can't cpme",Value=bool.FalseString
    }
}, "Choose an option")
    </p>
    <input type="submit"  value="Submit RSVP"/>

}

五、

@using (Html.BeginForm("action","controller",FormMethod.POST,bject htmlAttributes)) {} //提交到指定controller下的action中,并指定提交方式,并给form一个属性。
@using (Html.BeginForm("RsvpForm", "Home", FormMethod.Post, new {name="rsvpForm1" }))
{
   @* <p>Your name:@Html.TextBoxFor(x=>x.Name)</p>*@
    <p>Your email:@Html.TextBoxFor(x => x.Email)</p>
    <p>Your phone:@Html.TextBoxFor(x => x.Phone)</p>
    <p>
    Will you attend?
    @Html.DropDownListFor(x => x.WillAttend, new[]{
    new SelectListItem(){
        Text="Yes.I'll be there",Value=bool.TrueString
    },new SelectListItem(){
        Text="No,I can't cpme",Value=bool.FalseString
    }
}, "Choose an option")
    </p>
    <input type="submit"  value="Submit RSVP"/>

}

转帖:Html.BeginForm的更多相关文章

  1. Ajax.BeginForm方法 参数

    感谢博主 http://www.cnblogs.com/zzgblog/p/5454019.html toyoung 在Asp.Net的MVC中的语法,在Razor页面中使用,替代JQuery的Aja ...

  2. ASP.NET MVC Html.BeginForm 设置 timeout

    示例代码: @using (Html.BeginForm("PublishSubmit", "Blog", FormMethod.Post, new { id ...

  3. MVC Html.BeginForm 与 Ajax.BeginForm 使用总结

    最近采用一边工作一边学习的方式使用MVC5+EF6做一个Demo项目, 期间遇到不少问题, 一直处于研究状态, 没能来得及记录. 今天项目进度告一段落, 得以有空记录学习中遇到的一些问题. 由于MVC ...

  4. ASP.NET MVC 混搭 ASP.NET WebForms 所导致的 Html.ActionLink/BeginForm 问题

    首先,需要了解下这篇博文:<ASP.NET WebForms MapPageRoute 路由配置> 之前,在 ASP.NET MVC 中混搭 ASP.NET WebForms,使用 Map ...

  5. Ajax.BeginForm VS Html.BeginForm

    有的人说,AJAX一听,高大上,HTML一听,死老土,所以AJAX更好.其实这是错误的.每种方法有它不同的用途.现在做如下总结: @using (Ajax.BeginForm("Login& ...

  6. MVC之Ajax.BeginForm使用详解之更新列表

    1.首先,请在配置文件设置如下:(该项默认都存在且为true) <add key="UnobtrusiveJavaScriptEnabled" value="tru ...

  7. Ajax.BeginForm参数详解

    在Asp.Net的MVC中的语法,在Razor页面中使用,替代JQuery的Ajax使用,方便快捷. 使用Ajax.BeginForm方法会生成一个form表单,最后以Ajax的方式提交表单数据:需要 ...

  8. nginx负载均衡基于ip_hash的session粘帖

    nginx负载均衡基于ip_hash的session粘帖 nginx可以根据客户端IP进行负载均衡,在upstream里设置ip_hash,就可以针对同一个C类地址段中的客户端选择同一个后端服务器,除 ...

  9. MVC中自带的异步((Ajax.BeginForm)无效

    1.确定unobtrusive-ajax已经引用,VS2012带,2013不带 2.注意jq和unobtrusive-ajax引用顺序问题,确保jq在前 3.注意JQ和unobtrusive-ajax ...

随机推荐

  1. JQuery初识

    一.什么是JQuery       JQuery官方网站上是这样解释的:JQuery是一个快速简洁的JavaScript库,它可以简化HTML文档的元素遍历.事件处理.动画及Ajax交互,快速开发We ...

  2. windows系统中的dll的作用详细解释

    什么是.DLL文件? DLL 是一个包含可由多个程序同时使用的代码和数据的库.例如,在 Windows 操作系统中,Comdlg32 DLL 执行与对话框有关的常见函数.因此,每个程序都可以使用该 D ...

  3. get the execution time of a sql statement.

    declare @d datetimeset @d = GETDATE()select * from dbo.spt_valuesselect [语句执行花费时间(毫秒)]= DATEDIFF(ms, ...

  4. VS插件

    VS插件 背景 前些天去考科目二,感觉经历了一场不是高考却胜似高考的考试(10年前的5分之差, 还是难以释怀)!    一行八人,就我学的时间最少(4天,8人一辆车),教练都觉得我肯定还得再来一次! ...

  5. why-and-howto-calculate-your-events-per-second

    http://eromang.zataz.com/2011/04/12/why-and-howto-calculate-your-events-per-second/

  6. rsyslog 基本组成

    Facility 定义日志消息的来源,以方便对日志进行分类,facility 有以下几种: --kern 内核消息 --user 用户级消息 --mail 邮件系统消息 --daemon 系统服务消息 ...

  7. HDU1046:Gridland

    Problem Description For years, computer scientists have been trying to find efficient solutions to d ...

  8. C#复习一( Twenty Days)

      今天开始将要复习最近所学的一些C#知识.下面就来总结一下今天所复习的内容,以此来巩固我对C#知识的掌握.今天主要以举几个程序为例. 首先还是要注意代码的规范: –注释//,/**/,/// –骆驼 ...

  9. 如何最简单的优化MySql

    1.创建索引,一定要根据实际情况来创建,如果是连接表查询,如一个主帐号连接多个子帐号,可以考虑两个或三个以上的多索引: 2.合理利用时间排序,由于大多数表格用时间来排序,数据量相当大的时候,在时间列上 ...

  10. Java学习之equals和hashcode的关系

    两个对象值相同(x.equals(y) == true),但却可有不同的hash code,这句话对不对? 答:不对,如果两个对象x和y满足x.equals(y) == true,它们的哈希码(has ...