'ASP.NET MVC Equivalent to Response.Redirect Click Event
I have an html form in a view that needs to be reset from time to time. Problem is, fields enable/disable based on input. Therefore, the only way to truly reset the form is to reload the view (I would prefer that the entire page is reloaded). Due to several scenarios, simply refreshing does not work. I need the equivalent to Response.Redirect() and have the view redirect to itself... Haven't been able to find a good solution yet.
I have tried:
Adding an ActionResult in the controller that
public ActionResult ResetNoteReport() { return RedirectToAction("NoteReport"); }Setting a click event on the button itself that
onclick="window.location.href('<%= Url.Action("NoteReport")%>');"Removing input and setting values to null or "" via JQuery...
Among plenty of other stabs...
Solution 1:[1]
For some reason, my attempts at using window.location.href() was buggy earlier and would not reload the page. I don't know what was wrong with it before, but the following works perfectly fine:
<script type="text/javascript">
function reload() {
window.location.href = "<%=Url.Action("NoteReport") %>";
};
</script>
<%= Html.Button("reset", "Reset", HtmlButtonType.Button, "reload()") %>
Solution 2:[2]
just an aside but rather than change the page could you not use one of the jquery form utilities to clear the form?
Solution 3:[3]
Problem is, fields enable/disable based on input.
That is exactly one situation when MVC is not good to go with. WebForms perform much better in such scenarios.
Solution 4:[4]
<%= Html.Button("reset", "Reset", HtmlButtonType.Button, "window.location.reload()") %>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Braiam |
| Solution 2 | Pharabus |
| Solution 3 | User |
| Solution 4 | Tracker1 |
