1

I have a simple, one page, MVC application that has a form that will filter down what data is shown once the form has been submitted.

I want users to be able to copy and paste/bookmark the url to include these form values to be preselected.

EDIT: Including example

The user would only see http://localhost/ in their browser when I would like them to see http://localhost/?param1=4&otherItem=testing&test=true

This doesn't work obviously but not sure how I would accomplish this.

public ActionResult Index(int param1 = 0, string otherItem = "", bool test = false)
{

    return RedirectToAction("Index", "Home", new { param1, otherItem, test});
}
mason
  • 31,774
  • 10
  • 77
  • 121
user3953989
  • 1,844
  • 3
  • 25
  • 56

1 Answers1

1

You'll need to use JavaScript to generate the URL, since the values can change on the client and you don't want to make a call to the server for those. A downside of this technique is that it isn't tied into your routing structure. If you change your routes or query string parameter, you'll need to change the JavaScript to match. I don't know what your routes look like, but you mentioned using query string so I'll go based on that

<script type="text/javascript">
function generateUrl(){
    var param1 = document.getElementById("param1").value;
    var otherItem = document.getElementById("otherItem ").value;
    var test = document.getElementById("test ").value;
    return "@Url.Action("controller","action")?param1=" + param1 + "&otherItem=" + otherItem + "&test=" + test;
    //may need to URI encode the parameters
}
</script>

Then when form values change, catch that with JavaScript, call generateUrl() to get the URL to use, and then probably assign that value to a text box (perhaps with a copy to clipboard library) or hyperlink.

Community
  • 1
  • 1
mason
  • 31,774
  • 10
  • 77
  • 121