简体   繁体   English

如何将值从下拉项传递到URL?

[英]How to pass the value to URL from dropdown item?

I am asking this question again....because I am not getting correct answer... answers which I get is incorrect. 我再次问这个问题....因为我没有得到正确的答案...我得到的答案不正确。

I am developing MVC application and I am using razor syntax. 我正在开发MVC应用程序,并且正在使用razor语法。 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. 但我出错了。

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

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

I think error in this line... 我认为这条线有错误...

   url2 = url2.replace("d",'@d');

Issue solved Issue solved 解决的问题解决的问题

The problem in variable 'D' yes in "D". 变量“ D”中的问题是“ 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 当我在控制台窗口中看到它时,我使用了Google chrome的inspect element属性进行了检查。...单击该按钮时,我可以看到以下列方式形成的字符串

  http://localhost:54255/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 ? 看到上面链接中的最后一个字符,它不应该作为“ = D”来吗?

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 ? 这是Jquery中的错误吗?

Your variable d is not a server variable that can be called with "@" but a client variable set in javascript, so it should be user like : 您的变量d不是可以用“ @”调用的服务器变量,而是在javascript中设置的客户端变量,因此用户应为:

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

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

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

        }
    });
    return false;
});

(and the "@Model.Id" can be called directly in the Url.Action method). (以及“ @ Model.Id”可以直接在Url.Action方法中调用)。

Yes it is. 是的。 The problem is that the js replace function replaces every "d" character contained in the url2 string with your @d variable!!! 问题是js replace函数用@d变量替换url2字符串中包含的每个“ d”字符! You need to replace the "d" identifier with something that does not appear twice (or more) in the url string. 您需要用在URL字符串中不会出现两次(或多次)的东西替换“ d”标识符。 Besides, I think is better if you create the url directly using javascript and not the Razor helper. 此外,如果您直接使用javascript而不是Razor帮助器创建URL,我认为更好。 You can do this in one line of code instead of three. 您可以在一行代码中执行此操作,而不是三行代码。 Regards. 问候。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM