使用的HtmlHelper来获得BeginForm(动作)ASP.NET MVC 3的方法 [英] Use htmlhelper to get action in BeginForm() Method of ASP.NET MVC 3

查看:84
本文介绍了使用的HtmlHelper来获得BeginForm(动作)ASP.NET MVC 3的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在ASP.NET MVC 3中,我们总是用使用(@ html.BeginForm(){} 助手(假设使用不带任何参数)使用表单回发。

In ASP.NET MVC 3 we always use using(@html.BeginForm(){ } helper (assume using without any parameters) to use forms with postback.

返回的HTML,包括开放格式有一些属性标记和动作的再present的回传网址。

The returned html, includes an open form tag with some attributes and action that represent the postback url.

所以,当我重写我的自定义 BeginForm 帮助我需要这个网址。这动作属性不只是动作的名称或组合{地区} / {控制器} / {行动}

So when I overwrite my custom BeginForm helper I need this Url. This action attribute is not just action name or combination of {area}/{controller}/{action}.

我觉得这是一个相同的URL,我们用它来查看当前页面,因为当我们提出我们的支持,以同样的动作或相同的动作名称与 [HttpPost] 页属性。

I think this is a same url we use to see the current page, because when we submit page we backed to the same action or same action name with [HttpPost] attribute.

所以,我怎么可以从的HtmlHelper

推荐答案

您可以使用ILSpy或其他任何反射,看看什么是Html.BeginForm

You can use ILSpy or any other reflector and see what is happening in Html.BeginForm

我只是复制粘贴code你。

I just copy paste the code for you.

// System.Web.Mvc.Html.FormExtensions
/// <summary>Writes an opening &lt;form&gt; tag to the response. When the user submits the form, the request will be processed by an action method.</summary>
/// <returns>An opening &lt;form&gt; tag. </returns>
/// <param name="htmlHelper">The HTML helper instance that this method extends.</param>
public static MvcForm BeginForm(this HtmlHelper htmlHelper)
{
    string rawUrl = htmlHelper.ViewContext.HttpContext.Request.RawUrl;
    return htmlHelper.FormHelper(rawUrl, FormMethod.Post, new RouteValueDictionary());
}


// System.Web.Mvc.Html.FormExtensions
private static MvcForm FormHelper(this HtmlHelper htmlHelper, string formAction, FormMethod method, IDictionary<string, object> htmlAttributes)
{
    TagBuilder tagBuilder = new TagBuilder("form");
    tagBuilder.MergeAttributes<string, object>(htmlAttributes);
    tagBuilder.MergeAttribute("action", formAction);
    tagBuilder.MergeAttribute("method", HtmlHelper.GetFormMethodString(method), true);
    bool flag = htmlHelper.ViewContext.ClientValidationEnabled && !htmlHelper.ViewContext.UnobtrusiveJavaScriptEnabled;
    if (flag)
    {
        tagBuilder.GenerateId(htmlHelper.ViewContext.FormIdGenerator());
    }
    htmlHelper.ViewContext.Writer.Write(tagBuilder.ToString(TagRenderMode.StartTag));
    MvcForm result = new MvcForm(htmlHelper.ViewContext);
    if (flag)
    {
        htmlHelper.ViewContext.FormContext.FormId = tagBuilder.Attributes["id"];
    }
    return result;
}

这篇关于使用的HtmlHelper来获得BeginForm(动作)ASP.NET MVC 3的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆