asp.net-mvchidden-fieldurl.action

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


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.


Solution

  • 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();;
        });
    });