【问题标题】:How to pass parameters from @Url.Action to controller如何将参数从@Url.Action 传递给控制器
【发布时间】:2015-05-04 13:17:53
【问题描述】:

我有 buttonOnclick,它需要 2 个参数

$('.Conteiner').on("click", ".Button", function () {
        window.location.href = '@Url.Action("Form", "State", new {numberNights, datepicker})';
    });

我需要将参数从@Url.Action 传递给控制器​​

我的控制器:

public ActionResult Form(int numberNights, string datepicker)
        {
            @ViewBag.NumberNights = numberNights;
            @ViewBag.DatePicker = datepicker;
            return View();
        }

参数 numberNights 可以通过,但是 datepicker 总是为空。为什么?

【问题讨论】:

  • 试试这个window.location.href = "State/Form?numberNights="+numberNights"+&+"datepicker=" + datepicker;确保你的日期选择器有一个值!!

标签: jquery asp.net-mvc


【解决方案1】:

首先,您需要实际命名匿名对象的成员,即:

new { numberOfNights = numberOfNights, datepicker = datepicker }

numberOfNights,可能是纯属意外,因为它是动作的第一个参数。

其次,确保您没有混合客户端和服务器端代码。也就是说,要为 datepicker 传递值,它必须是在 Razor 代码中定义的 C# 变量,而不是 JavaScript 变量。该值也必须在您的 JavaScript 有机会运行之前存在。如果您需要在用户更改 datepicker 值时动态更新它,您需要放弃在 Url.Action 中传递它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-09-09
    • 2015-01-01
    • 2015-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多