0

I am kinda new to asp.net mvc website with jquery, razor, javascript and html. Right now my actionlink has a problem where I cannot insert the filter part, into ..page/?sortMethod=StartDate

filters?=pending generates from a button (pending is just 1 of the many status)

?sortMethod=StartDate generates from actionlink.

I am trying to make them work together to get: ..page/?filters?=pending&sortMethod=StartDate

I tried to do a script that tries to replace the

This is the initial code, sortMethod is a string.

   <script>
        $(function () {
            $("#filterUidClear").click(function () {
                $("#filterUid").val('');
            });
            $('#filterPending').checkboxradio();
            $('#filterDirect').checkboxradio();
            $('#ApplyFilter').click(function () {

                var params = "";
                if ($('#filterPending')[0].checked) {
                    params = "filters=pending";
                }
                if ($('#filterDirect')[0].checked) {
                    if (params !== "") {
                        params = params + "&";
                    }
                    params = params + "filters=direct";
                }
                $("#param").val("?" + params);
                window.location.href = "?" + params;
            });
        });
    </script>

@Html.ActionLink("Leave Date", "Index", new { sortMethod = ViewBag.StartDate })
This is the new modified one
@Html.ActionLink("Leave Date", "Index", new { sortMethod = ViewBag.StartDate }, new { id = action })
<script>
  $(function() {
      $('#action').click(function() {
          var filterparam = $("#param").val();
          this.href = this.href + '?filters=' + encodeURIComponent(filterparam) + '&sortMethod=' + @ViewBag.StartDate;

    });
</script>

I am trying to make them work together to get: ..page/?filters=pending&sortMethod=StartDate but to no avail. The table will display filtered with pending results and sorted by date

Right now it displays ..page/?action&sortingMethod=StartDate the table shows sorted by date but no filter and action is not being replaced by filter type e.g. ?filters=pending

DatB
  • 51
  • 6
  • add your HTML code as well – ramzan ali Oct 21 '19 at 06:20
  • `Index/filters?=pending&...` shouldn't be your *expected* result - it should be `Index?filters=pending&...` – freedomn-m Oct 21 '19 at 06:28
  • @freedomn-m alright it looks better I hope. So how do I resolve this? – DatB Oct 21 '19 at 06:39
  • add full of code in fiddle men.@BonBon – chandu komati Oct 21 '19 at 06:43
  • Clicking #action adds #param.val to the href. #param is set when you click on '#ApplyFilter, but #ApplyFilter also calls location.href = . So `$("#param").val()` will always `===""` otherwise the page would have reloaded. – freedomn-m Oct 21 '19 at 06:47
  • @chandukomati can't do man it – DatB Oct 21 '19 at 06:50
  • What does this give you: `console.log($("#action").length)` I suspect 0 – freedomn-m Oct 21 '19 at 06:50
  • @freedomn-m yeah I get that the param information will be lost after it gets out of the script and goes into the next script. I have been trying to think of ways to get the variable value passed into the second script but does not seems like it is working. Any other alternatives I can go for if I do not want to use actionlink ? – DatB Oct 21 '19 at 06:51
  • It won't be lost as you have `$("#param").val("?" + params)`. Just you're not "going out of one script into the second script" - your "going out of the page to another page" (with the same address - ie you're reloading the page) – freedomn-m Oct 21 '19 at 06:54
  • Ah I get it, right. – DatB Oct 21 '19 at 06:56
  • You want to keep the existing parameters on the url when creating the action link. It's not straightforward, but this answer does just that: https://stackoverflow.com/a/3786531/2181514 – freedomn-m Oct 21 '19 at 07:20
  • Thanks I'll go read it up! – DatB Oct 21 '19 at 07:24

0 Answers0