How to append a querystring to the url created by Url.Action base on a hidden fields values

Steve picture Steve · Aug 23, 2016 · Viewed 8.7k times · Source

Here is my controller Action method

public ActionResult ChangeStateId(int userId,int stateId)
 {
   return RedirectToAction("Index")    
 }

and in my anchor tag of View, i want to redirect to above action method with parameter values like this

<a href="'@Url.Action("ChangeStateId","User")?userId='+$('#hidID').val()+ '&stateId=' +$('#hidStateId'.val()")></a>;

but it is not working for me.

Answer

RAM picture RAM · Aug 23, 2016

Use this a Tag:

<a id="GoToRedirectAction" data-url="@Url.Action("NewTelegramHlink", "Hlink",null, Request.Url.Scheme)">Go To Redirect Action</a>

With these jQuery codes:

$(document).ready(function() {
    $('a#GoToRedirectAction').click(function() {
        window.location.href = $(this).data('url') + "?userId=" + $('#hidID').val() + "+&stateId=" + $('#hidStateId').val();;
    });
});

Or

$(document).ready(function() {
    $('body').on("click",'a#GoToRedirectAction',function() {
        window.location.href = $(this).data('url') + "?userId=" + $('#hidID').val() + "+&stateId=" + $('#hidStateId').val();;
    });
});