【问题标题】:Use htmlhelper to get action in BeginForm() Method of ASP.NET MVC 3使用 htmlhelper 在 ASP.NET MVC 3 的 BeginForm() 方法中获取操作
【发布时间】:2012-04-21 14:50:12
【问题描述】:

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

返回的 html 包含一个带有一些属性的打开的 form 标记和代表回发 URL 的 action

所以当我覆盖我的自定义 BeginForm 助手时,我需要这个 Url。这个action 属性不仅仅是动作名称或{area}/{controller}/{action} 的组合。

我认为这是我们用来查看当前页面的相同 url,因为当我们提交页面时,我们使用 [HttpPost] 属性支持相同的操作或相同的操作名称。

那么我怎样才能从HtmlHelper 获得这个值呢?

【问题讨论】:

  • 只是出于好奇,您需要自定义 BeginForm 来做什么?

标签: asp.net-mvc asp.net-mvc-3 razor html-helper


【解决方案1】:

使用 htmlhelper 参数

public class myBeginForm : IDisposable
{
    private HtmlHelper _myHtmlhelper;
    public myBeginForm (HtmlHelper htmlHelper, [you can add your need argument here] )
    {
        _myHtmlhelper= htmlHelper;
        var container = new TagBuilder("form");

       /// your Code 
    }

    public void Dispose()
    {
        myHtmlhelper.ViewContext.Writer.Write("</form>");
    }
}

【讨论】:

  • 我不想添加任何论据,我在问题中提到,我需要从htmlhelper 获得价值,我相信有任何方法可以得到它,如您所见@987654323 @得到它
【解决方案2】:

您可以使用 ILSpy 或任何其他反射器并查看 Html.BeginForm 中发生的情况

我只是为你复制粘贴代码。

// 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;
}

【讨论】:

  • 这似乎是一个很好的答案。您可能应该这样标记它。
【解决方案3】:

如果你想从@Html.BeginForm() 获得一个不带任何参数的动作属性,你可以使用jquery。我使用它在 jqueryUI 对话框中 ajax 发布表单。

var form = $('form'); //get the form
var actionUrl = $('form').attr('action'); //get the action url

然后你可以使用 POST

 $.ajax({
      type: "POST",
      url: actionUrl,
      data: form.serialize(),                                    
      success: function (data, status, xhr) {
                if (data.Sucess) {
                   //do something
               } else {
               }
      }
})  

问候。

【讨论】:

    猜你喜欢
    • 2012-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-15
    • 2012-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多