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?
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.
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.
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.
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.
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"/>
}
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"/>
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With