Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

asp.net mvc Ajax.BeginForm

I'm having some difficulties with Ajax.BeginForm

I have something like this in a view

  <% using (Ajax.BeginForm("ActionName", null , null, new { id = "FormName" }))
     {%>
      <input type="hidden" value = '<%= Html.Encode( Model.id) %>' name="id"/>
      <textarea id="message" name=message rows="4" style="width: 90%"> 
      </textarea>
  <% }%}

And the action method is something like this

    [AcceptVerbs(HttpVerbs.Post)]
    [Authorize]
    public ActionResult ActionName(int id, string message)
    {
     ....
    }

I'm trying to pass the 'id' and 'message' to the action method. I'm passing 'null' for routeValues but I dont know what to pass. Ideally I was trying to find an overload that did not require route values but took actionName and htmlattributes (for form name) but I could not find one.I don't want to add 'message' to the view-model and I do need the FormName in there for jquery operations. What is the best way to work around this problem ?

Oh, I forgot to mention, This is how I post the form

 $.post($("#FormName").attr('action'), $("#FormName").serialize(),
                               function(result) {
                                   $("#correspondingDiv").html(result);
                               }
                            );
like image 231
Bala R Avatar asked Apr 08 '10 13:04

Bala R


People also ask

What is Ajax BeginForm in MVC?

Ajax. BeginForm is the extension method of the ASP.NET MVC Ajax helper class, which is used to submit form data to the server without whole page postback. To work Ajax. BeginForm functionality properly, we need to add the reference of jquery.

What is @using HTML BeginForm ()) in MVC?

"BeginForm()" is an extension method for both HtmlHelper and AjaxHelper classes. It returns an MVCForm object from both HtmlHelper and AjaxHelper class instances so there is not much difference but the AjaxHelper method submits the form asynchronously using JavaScript.

What is difference between HTML BeginForm and Ajax BeginForm?

BeginForm() will create a form on the page that submits its values to the server as a synchronous HTTP request, refreshing the entire page in the process. Ajax. BeginForm() creates a form that submits its values using an asynchronous ajax request.

Why we use HTML BeginForm in MVC?

Html. BeginForm is the Html Helper Extension Method that is used for creating and rendering the form in HTML. This method makes your job easier in creating form.


1 Answers

Use this overload: http://msdn.microsoft.com/en-us/library/dd470605.aspx

Ajax.BeginForm(
    string "ActionName",
    string "ControllerName",
    new routevalues {id="IDValue",message="MyMessage"},
    new AjaxOptions {OnBegin=[someFunction], OnFailure=[failureFunction] },
    new { id = "FormName" }
)
like image 158
Dave Swersky Avatar answered Oct 06 '22 09:10

Dave Swersky