MVC Html.ActionLink ignores the Controller parameter
Asked Answered
S

1

6

I have a a view that has the following code:

<h2><%= Model.Company.CompanyName %></h2>
<h3>Projects</h3>
<ul>
<%
    foreach (Project p in Model.Company.Projects)
    {
        %>
        <li><%= Html.ActionLink(p.ProjectName,"Details", "Projects", new {id=p.ProjectID,companyId=p.CompanyID}) %></li>
        <%   
    } 
%>
</ul>
<%= Html.ActionLink("Add Project", "Create", "Projects", new {id = Model.CompanyID}) %>
<br />
<h3>Users</h3>

I have a ProjectsController but when I run the application and click on the Add Project Link it expects to go to /Company/Create instead of /Projects/Create. Am I missing something?

Sediment answered 11/2, 2010 at 1:16 Comment(0)
E
13

You're matching the signature that expects the route values in the third parameter and the html attributes in the fourth. Add another parameter (null is ok) and you'll get the signature that has the link text, action, controller, route values, and html attributes.

<%= Html.ActionLink("Add Project",
                    "Create",
                    "Projects",
                    new {id = Model.CompanyID},
                    null ) %>
Esquibel answered 11/2, 2010 at 1:49 Comment(2)
Thanks I had just figured that one out!Sediment
@Susan - since you're new here, I thought I'd mention that the way SO works is you vote up (using the arrow buttons next to the question) answers that are helpful. Then, select the best answer to your question and accept it using the check mark to signify that it solved your problem. This way others with the same problem know what the best solution is.Esquibel

© 2022 - 2024 — McMap. All rights reserved.