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?

1
  • Just out of curiosity, what do you need a custom BeginForm for? Commented Apr 9, 2012 at 9:43

3 Answers 3

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;
}
Sign up to request clarification or add additional context in comments.

1 Comment

That seems like a good answer. You should probably mark it as such.
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.

Comments

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

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.