0

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?

gideon
  • 19,329
  • 11
  • 72
  • 113
Saeid
  • 13,224
  • 32
  • 107
  • 173

3 Answers3

3

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;
}
HashCoder
  • 946
  • 1
  • 13
  • 32
0

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.

Matija Grcic
  • 12,963
  • 6
  • 62
  • 90
-1

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>");
    }
}
MHF
  • 511
  • 8
  • 22
  • I don't want to add any argument, I mentioned at the question, I need to get value from `htmlhelper`, I am sure there is any way to get it, as you see `BeginForm()` get it – Saeid Apr 09 '12 at 07:34