0

I've got 2 views: Question and Answer. From the Question View, Detail Action I wanna redirect to Answer View, Create Action, so I've placed:

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

and in Answer View:

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

But the id in Create(string id) is always null. How can I pass this value properly?

2 Answers 2

3

You are using a wrong overload of the ActionLink helper. It should be:

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

which would generate

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

whereas you are using:

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

which generates:

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

I think that now it's not difficult to understand why your anchor doesn't work.

Sign up to request clarification or add additional context in comments.

Comments

0

You seem to have chosen wrong ActionLink overload. Try this:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.