asp.net-mvc-3 – 在MVC 3.0中重写Html.BeginForm()并保持不引人注目的javascript

这似乎是一种愚蠢的努力,但这是我想要学习的东西.

现在,在ASP.NET MVC 3.0中,您需要使用语法@using(Html.BeginForm()){然后再使用}来关闭表单块以获得新的“不引人注目的javascript”,以免您想要手工编写所有这些(很好).

出于某种原因(阅读:* OCD *)我不喜欢这样.我真的宁愿这样做..

@Html.BeginForm()
<div class="happy-css">
</div>
@Html.EndForm()

好像傻了吗?是的,对每个人都很好.我想知道为什么它如何工作并根据自己的喜好塑造它.所以我认为我开始挖掘的第一个地方是MVC 3.0源代码本身.所以我跳进了codeplex来找到BeginForm Extension方法.

(http://aspnet.codeplex.com/SourceControl/changeset/view/63452#288009)

所以现在我对如何开始实现目标感到有些困惑.通过阅读代码,我发现它们都归结为根方法(这并不奇怪,因为大多数扩展方法似乎都是分层方法,所有这些方法都是为了避免冗余而分成单个方法).

private static MvcForm FormHelper(this HtmlHelper htmlHelper,string formAction,FormMethod method,IDictionary<string,object> htmlAttributes) {
            TagBuilder tagBuilder = new TagBuilder("form");
            tagBuilder.MergeAttributes(htmlAttributes);
            // action is implicitly generated,so htmlAttributes take precedence.
            tagBuilder.MergeAttribute("action",formAction);
            // method is an explicit parameter,so it takes precedence over the htmlAttributes.
            tagBuilder.MergeAttribute("method",HtmlHelper.GetFormMethodString(method),true);

            HttpResponseBase httpResponse = htmlHelper.ViewContext.HttpContext.Response;
            httpResponse.Write(tagBuilder.ToString(TagRenderMode.StartTag));
            return new MvcForm(htmlHelper.ViewContext.HttpContext.Response);
        }

在这里没有看到的是这种方法与不显眼的javascript有什么关系.如果我只是输入..

< form action =“/ Controller / Action”method =“post”>

然后像我这样进行验证……

@ Html.ValidationSummary(假)

我没有得到不引人注目的JavaScript.但是,如果我使用

@using(Html.BeginForm()){然后我做.我甚至检查了生成标记,我真的找不到差异.

现在它变得奇怪了.如果我输入…

@ Html.BeginForm(),然后把我所有的表单代码,表单工作,我得到不引人注目的JavaScript,但我必须手动输入< / form>在末尾. @ Html.EndForm()不起作用.但最重要的是,我现在将文本System.Web.Mvc.Html.MvcForm写入< form action =“/ Controller / Action”method =“post”>正下方的输出流中. HTML.

有人可以启发和/或帮助我吗?

解决方法

您的基本问题(即如何使用BeginForm / EndForm语法)的答案是以下列方式执行此操作:
@{ Html.BeginForm(...); }
<div> content</div>
@{ Html.EndForm(); }

不幸的是,当调用写入输出的帮助程序时,Razor语法现在更加冗长(与大多数只返回HTML代码段的帮助程序相反).您可以通过编写自己的扩展方法来使这更容易:

public static IHtmlString FormBegin(this HtmlHelper helper,...) {
    helper.BeginForm(...);
    return new HtmlString("");
}

public static IHtmlString FormEnd(this HtmlHelper helper) {
    helper.EndForm();
    return new HtmlString("");
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


这篇文章主要讲解了“WPF如何实现带筛选功能的DataGrid”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“WPF...
本篇内容介绍了“基于WPF如何实现3D画廊动画效果”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这...
Some samples are below for ASP.Net web form controls:(from http://www.visualize.uk.com/resources/asp
问题描述: 对于未定义为 System.String 的列,唯一有效的值是(引发异常)。 For columns not defined as System.String, the only vali
最近用到了CalendarExtender,结果不知道为什么发生了错位,如图在Google和百度上找了很久,中文的文章里面似乎只提到了如何本地化(就是显示中文的月份)以及怎么解决被下拉框挡住的问题,谈
ASP.NET 2.0 page lifecyle ASP.NET 2.0 event sequence changed a lot since 1.1. Here is the order: App
静态声明: &#39; Style=&quot;position: relative&quot; AppendDataBoundItems=&quot;True&quot;&gt; (无 或 空 或
以下内容是从网络上搜集资料,然后整理而来的。不当之处,请不吝指教。(The following were from network, and edited by myself. Thanks in a
Imports System Imports System.Reflection Namespace DotNetNuke &#39;*********************************
Ok so you have all seen them: “8 million tools for web development”, “5 gagillion tools that if you
以下内容来源于: http://blog.csdn.net/cuike519/archive/2005/09/27/490316.aspx 问:为什么Session在有些机器上偶尔会丢失? 答:可能和
以下文章提到可以用“http://localhost/MyWebApp/WebAdmin.axd”管理站点: ---------------------------------------------
Visual Studio 2005 IDE相关的11个提高开发效率的技巧 英文原创来源于: http://www.chinhdo.com/chinh/blog/20070920/top-11-vis
C#日期格式化 from: http://51xingfu.blog.51cto.com/219185/46222 日期转化一 为了达到不同的显示效果有时,我们需要对时间进行转化,默认格式为:2007
from: http://www.nikhilk.net/UpdateControls.aspx Two controls that go along with the UpdatePanel and
Open the report in the Designer. In the ToolBox, select/expand the &quot;Report Items&quot; section.
from: http://drupal.org/node/75844 Do this: find which TinyMCE theme you are using. For the sake of
asp.net中给用户控件添加自定义事件 用户控件中定义好代理和事件: public delegate void ItemSavedDelegate(object sender, EventArgs
在Windows版本的Safari中浏览以下的页面。 http://www.asp.net/AJAX/Control-Toolkit/Live/Calendar/Calendar.aspx Calen
http://aspnet.4guysfromrolla.com/articles/021506-1.aspx By Scott Mitchell Introduction When creating