I have the following in my Action:
[AjaxException]
public ActionResult DoSomething(sring someParam1, string someParam2) {
//do whatever you need to do here, db etc
return new EmptyResult();
}
in my html
<form id="search-frm" name="search-frm" action="@Url.Action("DoSomething", "MyActions")" method="post" >
<input type="button" id="search-btn" value="search" class="btn" onclick="DoSomething();return false;" />
<input type="text" name="param1" id="param1" />
<input type="text" name="param2" id="param2" />
</form>
in my JS
function DoSomething() {
$("#search-frm").submit();
return false;
}
When I click on the button, and after the controller action DoSomething is done, I get redirected to MyActions/DoSomething. Is there a way to not have that w/o using jquery $.ajax? I simply need to do something and not go away from the existing page.
because your code is so. When you click on the button you are calling the
DoSomethingjavascript function and inside that you are submitting the form. So it is same as the normal form submit(clicking on the submit button to submit). that is the reason it is redirecting (actually being posted toDoSomethingaction.If you do not want to navigate away from the current page, you may use
ajaxto do your posting and get result and stay in the same page. So i would make changes to your code like this1) Get rid of the OnClick event binding from the HTML markup
2) Add this javascript which handles the form submit
Not sure why return
EmptyResultfrom the Action method. You may need to return some valid response which indicates the status of the Action you are trying to perform.You may keep a generic
ViewModelto return such results and use that, instead of dynamically typing like above.and in your action method