7

I am having an issue with Html.ActionLink not giving me the link I was expecting.

Here is my route:

routes.MapRoute(
    "KnownCustomer", // Route name
    "{controller}/{action}/{custId}/{projId}", // URL with parameters
        new { controller = "Home", action = "Index" }, // Parameter defaults
    new { custId = @"\d+", projId = @"\d+" }
);

Here is my call to Html.ActionLink()"

@Html.ActionLink("Create New", "Create", "Conflict")

Here is the URL of the page that the ActionLink exists on: http://localhost:1283/Conflict/Index/1200/300 Here is the result of the call to the above Html.ActionLink() http://localhost:1283/Conflict/Create

Shouldn't the call include the other route parameters? What I was expecting was http://localhost:1283/Conflict/Create/1200/300

Do I need to pass the custId and projId in to the View and use an overload to supply the values manually?

1 Answer 1

11

You've not specified that the action link should have any parameters, in that case it's a straight URL to the action. You need to include the parameters, i.e.

@Html.ActionLink("Create New", "Create", "Conflict", new { custID = Model.custID, projID  = Model.projID }, null)
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! Now I need an opinion. It is a create method. The model (view model) is empty. Do I create an instance of it and pass it in, or do I just use the ViewBag?
@Dean: That's very much a matter for choice, for two values that you use once for an ActionLink then I'd be tempted to use ViewData but if you want to be clean then passing in an object model providing the appropriate properties would be cleaner in the strictest sense.

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.