views:

4230

answers:

5

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... Any help would be greatly apreciated! Thanks!

A: 

Response.Redirect should still work, but try feeding it Request.Url as the parameter, like this:

Response.Redirect(Request.Url);

That said, you should consider reseting the form using javascript. One less Response.Redirect means one less roundtrip to the server which is a better user experience.

BTW, I'm guessing that refreshing doesn't work for you because the page is caching the values.

ajma
+1  A: 

Sorry to waste your time with this. For some reason, my attempts at using window.location.href() was buggy earlier and would not reload the page. When I got back to it, I attempted your suggestion, but ended up back to the window.location.href(). End result, 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()") %>

Thanks again for the quick responses! I love this site!

BueKoW
try window.location.reload() if all you want is the current location reloaded.
Tracker1
window.location.replace(window.location.href) should work, but will remove the original entry in the browser history.
Tracker1
A: 

just an aside but rather than change the page could you not use one of the jquery form utilities to clear the form?

Pharabus
I was getting close with that, but our application is for a base of roughly 300 users and we are more concerned about clean code so long as we aren't being ridiculous with server calls. Using window.location.href required 1 line where using JQuery to clear the ugly form I'm dealing with would have taken a few at the least...
BueKoW
A: 

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.

User
Agreed. As of now, I miss webforms quite a bit. However, the more I work with MVC and understand it, the more I see the benefits.
BueKoW
A: 
<%= Html.Button("reset", "Reset", HtmlButtonType.Button, "window.location.reload()") %>
Tracker1
Thanks for the suggestion. I tried this, but it doesn't work when I attempt to submit the form with bad data. The server validation returns the form with the bad data persisted and info as to why the submission failed. After clicking the Reset button you suggested above, the form does not reload to it's original state.
BueKoW
Gotcha, realized this on retrospect, since it's just refreshing the last request, not forcing to the same url as a get...
Tracker1
Is there a new release of MVC that I don't know of? I installed the framework when NerdDinner came out and Html.Button is not available (unresolved symbol).
Pawel Krakowiak