How to pass the selected dropdown value to a URL i

2019-07-17 05:07发布

问题:

I am developing MVC application and I am using razor syntax. I am trying to get the selected item from dropdown list value and to pass it to the controller method.

but I am getting error.

The name 'd' does not exist in the current context

The Updated Code is...

   $("#btnForword").click(function(){
    d = $('#HODList').val()
    alert(d);

    var url2 = "@Html.Raw(Url.Action("SendPaymentAdviceForApproval", "PaymentAdvice", new { paymentAdviceId = "idValue" , nHOD = "@@D@@" }))"
    url2 = url2.replace("idValue",'@Model.Id');
    url2 = url2.replace("@@D@@",d);

    $.ajax({
        url: url2, type: "POST", success: function (data) {
            $("#btnForword").css("display","none");

        }
    });
    return false;
});

Issue solved Issue solved

The problem in variable 'D' yes in "D".

I checked using inspect element property of Google chrome, when I saw it in console window.... When I click on the button , I can see the string formed in below way

ht...../PaymentAdvice/SendPaymentAdviceForApproval?paymentAdviceId=304&nHO8=D

jquery-1.7.1.min.js:4

see the last character in the above link, it should not be come as a "=D" isnt it ?

I used the below code...and it works perfectly.

$("#btnForword").click(function(){

            var url2 = "@Html.Raw(Url.Action("SendPaymentAdviceForApproval", "PaymentAdvice", new { paymentAdviceId = "idValue" , nHOD = "HODId" }))";
            url2 = url2.replace("idValue",'@Model.Id');
            url2 = url2.replace("HODId",$('#HODList').val());


            $.ajax({
                url: url2, type: "POST", success: function (data) {
                    $("#btnForword").css("display","none");

                }
            });
            return false;
        });

Is this a bug in Jquery ?

回答1:

You should use data property of $.ajax to pass parameter to controller-action.

$("#btnForword").click(function(){
    var d =  document.getElementById("HODList").value;

    // define url
    var url2 = 'Url.Action("SendPaymentAdviceForApproval", "PaymentAdvice")';
    //url2 = url2.replace("idValue",'@Model.Id');
    //url2 = url2.replace("d",'@d');

    $.ajax({
        url: url2, 
        type: "POST", 
        // add parameters in here...
        data: { paymentAdviceId : @Model.Id, nHOD : d }
        success: function (data) {
            $("#btnForword").css("display","none");
        }
    });

    return false;
});

And your controller should looks:

public ActionResult SendPaymentAdviceForApproval(int paymentAdviceId, int nHOD){...}


回答2:

Try this

$(function(){

$("#btnForword").click(function(){
    var d =  $('#HODList').val();

    var url2 = "@Html.Raw(Url.Action("SendPaymentAdviceForApproval", "PaymentAdvice", new { paymentAdviceId = "idValue" , nHOD = "@@D@@" }))";
    url2 = url2.replace("idValue",'@Model.Id');
    url2 = url2.replace("@@D@@",d);

    $.ajax({
        url: url2, type: "POST", success: function (data) {
            $("#btnForword").css("display","none");

        }
    });
    return false;
   });
});

don't consider replacing "d" as it will affect your other part of url as well.. Try specific placeholder instead. If this not works... Then can you please show Signature of your Action method too?

Your controller action should be like this

[HttpPost]
public JsonResult SendPaymentAdviceForApproval(int paymentAdviceId, int nHOD)
{
   // Your existing logic here... 
}

Cheers