Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a class to an Ajax.BeginForm Call in Razor View Model

I have a the following code

@using (Ajax.BeginForm(new AjaxOptions { UpdateTargetId = "update_panel", Url = "/Part/SearchPart" }))
        {
            <input type="text" placeholder="Search Parts"/>
            <input type="submit" value="Search"/>
        }

it outputs the following HTML

<form action="/Part" data-ajax="true" data-ajax-mode="replace" data-ajax-update="#update_panel" data-ajax-url="/Part/SearchPart" id="form0" method="post">                        
    <input type="text" placeholder="Search Parts"/>
        <input type="submit" value="Search"/>
</form>

and I would like to have the outputted HTML to have the ,form> tag to have the class="pull-right". How can I accomplish this?

like image 545
PlTaylor Avatar asked Jan 12 '12 16:01

PlTaylor


People also ask

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.

What does Ajax BeginForm do?

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.

What is @using 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. Here, is the method to create a form using Html.

Which two objects can you passed as a parameter to the HTML helper Dot BeginForm method?

In the "BeginForm()" method we passed an action name and controller name so that the action method will be called that handles the post data request.


2 Answers

You could use one of the overloads that allows you to specify html attributes::

@using (Ajax.BeginForm(null, null, new AjaxOptions { UpdateTargetId = "update_panel", Url = "/Part/SearchPart" }, new { @class = "pull-right" }))
{
    <input type="text" placeholder="Search Parts"/>
    <input type="submit" value="Search"/>
}

Also I would more than strongly recommend you relying on the url of your form instead of hardcoding it in the AjaxOptions because when you deploy your application in a virtual directory chances are that your hardcoded /Part/SearchPart url will not work. Not to mention if you change your route patterns in Global.asax. So:

@using (Ajax.BeginForm("SearchPart", "Part", null, new AjaxOptions { UpdateTargetId = "update_panel" }, new { @class = "pull-right" }))
{
    <input type="text" placeholder="Search Parts"/>
    <input type="submit" value="Search"/>
}
like image 71
Darin Dimitrov Avatar answered Oct 07 '22 14:10

Darin Dimitrov


You have to put you action and controll to refer from your form.

here is the example:

@using (Ajax.BeginForm("LogOn","Account", new AjaxOptions { UpdateTargetId = "update_panel", Url = "/Part/SearchPart" }, new { @class ="pull-right"}))
{
    <input type="text" placeholder="Search Parts"/>
    <input type="submit" value="Search"/>
}
like image 42
hackp0int Avatar answered Oct 07 '22 14:10

hackp0int