Use htmlhelper to get action in BeginForm() Method of ASP.NET MVC 3

最后都变了- 提交于 2020-01-03 05:08:13

问题


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

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

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}.

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.

So how can I get this value from HtmlHelper?


回答1:


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

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



回答2:


If you want an action attribute from @Html.BeginForm() without any parameters you can use jquery. I use this to ajax post a form inside jqueryUI dialogs.

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

And then you can use POST

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

Regards.




回答3:


use htmlhelper argument

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>");
    }
}


来源:https://stackoverflow.com/questions/10069687/use-htmlhelper-to-get-action-in-beginform-method-of-asp-net-mvc-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!