如何将 jquery 值发送​​到控制器..?

发布于 2024-12-08 20:08:31 字数 820 浏览 3 评论 0原文

我在视图中有一个用 jquery 处理的下拉列表。我需要知道如何将下拉列表中选定的项目发送到控制器,因为我需要此信息来执行特定操作,任何人都可以帮助我..?

这是我尝试过的 javascript 代码,但它不起作用:

  $("#BtnPassType").click(function () {
                    var formData = $("#displaydropdown option:selected").val();

                    $.post("/EquipodeRed/Prueba/", { tipo: formData }, 
                    function (data) {
                        alert(data.toString());
                    }, "text");

                });

这是按钮的 HTML 代码:

<input type="button" id="BtnPassType" value="Send Type" />

这是控制器代码

public String Prueba(string tipo)
        {
            string ubic = "Esta es la Ubicacion del equipo de tipo :";
            return ubic + tipo;
        }

我感谢一些帮助,提前致谢。 问候。

I have in the view a dropdownlist that I handle with jquery. I need to know how to send the selected Item of the dropdown to the controller, because I need this information to do a specific action, could anybody help me..??

Here's the javascript code I tried, but it doesn't work:

  $("#BtnPassType").click(function () {
                    var formData = $("#displaydropdown option:selected").val();

                    $.post("/EquipodeRed/Prueba/", { tipo: formData }, 
                    function (data) {
                        alert(data.toString());
                    }, "text");

                });

Here's the HTML code of the button:

<input type="button" id="BtnPassType" value="Send Type" />

And here's the Controller Code

public String Prueba(string tipo)
        {
            string ubic = "Esta es la Ubicacion del equipo de tipo :";
            return ubic + tipo;
        }

I appreciate some help, thanks in advance.
Regards.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

李白 2024-12-15 20:08:31

你的 AJAX 调用看起来没问题。您可以像这样简化它:

$('#BtnPassType').click(function () {
    var formData = $('#displaydropdown').val();
    $.post('/EquipodeRed/Prueba', { tipo: formData }, function (data) {
        alert(data);
    });
    // make sure you return false to cancel any default actions of this button
    return false;
});

另外,最好让控制器操作返回操作结果:

public ActionResult Prueba(string tipo)
{
    return Content("The selected type is :" + tipo);
}

当然,避免在 JavaScript 中硬编码 url,始终使用 url 助手:

$.post('@Url.Action("Prueba", "EquipodeRed")', { tipo: formData }, function (data) {
    alert(data);
});

You AJAX call seems fine. You could simplify it like this:

$('#BtnPassType').click(function () {
    var formData = $('#displaydropdown').val();
    $.post('/EquipodeRed/Prueba', { tipo: formData }, function (data) {
        alert(data);
    });
    // make sure you return false to cancel any default actions of this button
    return false;
});

Also it is better to have controller actions return action results:

public ActionResult Prueba(string tipo)
{
    return Content("The selected type is :" + tipo);
}

And of course avoid hardcoding urls in your javascripts, always use url helpers:

$.post('@Url.Action("Prueba", "EquipodeRed")', { tipo: formData }, function (data) {
    alert(data);
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文