0

我有 2 个视图:问题和答案。从问题视图,详细操作我想重定向到答案视图,创建操作,所以我放置了:

@Html.ActionLink(Model.QuestionId.ToString(), "Create", "Answer", "Answer", new { id = Model.QuestionId })

在答案视图中:

public ActionResult Create(string id)
{
    (...)
    return View();
} 

但是 Create(string id) 中的 id 始终为空。我怎样才能正确传递这个值?

4

2 回答 2

3

您使用了错误的 ActionLink 帮助程序重载。它应该是:

@Html.ActionLink(
    Model.QuestionId.ToString(),     // linkText
    "Create",                        // actionName
    "Answer",                        // controllerName
    new { id = Model.QuestionId },   // routeValues
    null                             // htmlAttributes
)

这会产生

<a href="/answer/create/123">123</a>

而您正在使用:

@Html.ActionLink(
    Model.QuestionId.ToString(),     // linkText
    "Create",                        // actionName
    "Answer",                        // controllerName
    "Answer",                        // routeValues
    new { id = Model.QuestionId }    // htmlAttributes
)

生成:

<a href="/Answer/Create?Length=6" id="123">123</a>

我认为现在不难理解为什么你的锚不起作用。

于 2012-08-17T22:33:42.797 回答
0

您似乎选择了错误的ActionLink重载。尝试这个:

@Html.ActionLink(Model.QuestionId.ToString(), "Create", "Answer", new { id = Model.QuestionId }, null)
于 2012-08-17T22:33:35.077 回答