通过JSON传递方法的参数

时间:2014-06-06 13:09:48

标签: c# .net json asp.net-mvc-4

我正在调用方法如下:

public JsonResult RenderTo_Observation(string AccountID)
{
    try
    {

        return Json(new
        {
            redirectUrl = Url.Action("Observation", "PP",  AccountID),
            isRedirect = true
        });
    }
    catch (Exception)
    {
        throw;
    }
}

我在PP控制器中调用观察方法,其定义如下:

public ActionResult Observation(string AccountID)
{
    ViewBag.AccountID = AccountID;
    return View();
}

我正在通过accountid参数,因为我们可以看到它:

redirectUrl = Url.Action("Observation", "PP",  AccountID)

在此我确保AccountID具有所需的值。

但是当通过它调用函数Observation时,

它没有显示我传递的AccountID参数值。

每次调用函数时它都显示为null。

上述代码中的错误是什么?

请帮帮我。

1 个答案:

答案 0 :(得分:1)

您需要将操作参数作为对象传递:

redirectUrl = Url.Action("Observation", "PP", new { AccountID })

这允许UrlHelper使用正确的名称创建参数。

相关问题