2

I have following two controllers: HomeController and DocGenController. The HomeController is the default controller in my project. I would like to open with a button-click a function in my DocGenController. For example: I created on my page a top-navigation. I used this code to open the DocGen.cshtml- page (via the controller HomeController): @Html.ActionLink("Generator", "DocGen", "Home").

Ok, now I tried this: @Html.ActionLink("Add Value", "AddValue", "DocGen", new { @class = "btn btn-primary" }) but I get every time this Error: The resource (/Home/AddValue) can be not found (Error: 404) .

Why he try to open /Home/AddValue ?!

How can I use my DocGenController?

Thanks for your help

4
  • @Html.ActionLink("Add Value", "Home", "DocGen", new { @class = "btn btn-primary" },new {param1=1,param2=2}) action link has many overloads, the one you are using , actionlink('title','controller','action',new{other parameters etc}) Commented Jul 19, 2016 at 8:55
  • As per your code @Html.ActionLink("Generator", "DocGen", "Home") Home is the controller and DocGen is action. Please swap them and try. Commented Jul 19, 2016 at 9:01
  • @user3151766 its not working Commented Jul 19, 2016 at 9:07
  • Sorry, I meant to use as : @Html.ActionLink("Generator", "YOUR FUNCTION NAME", "DocGen") Commented Jul 19, 2016 at 9:11

2 Answers 2

2

If you want you can also use a HTML <a>-Tag and in the href you just put: href='@Url.Action("AddValue", "DocGen")'

So the complete <a> would be:

<a href='@Url.Action("AddValue", "DocGen")' class="btn btn-primary">Add Value</a>
Sign up to request clarification or add additional context in comments.

1 Comment

it's also a preferable solution if you need to integrate url within some more complicated structure :)
1

You're using the wrong overload of Action Link method, as you can see here: https://msdn.microsoft.com/en-us/library/system.web.mvc.html.linkextensions.actionlink(v=vs.118).aspx

The signature of the simplest method including controller name is:

public static MvcHtmlString ActionLink(
    this HtmlHelper htmlHelper,
    string linkText,
    string actionName,
    string controllerName,
    object routeValues,
    object htmlAttributes
)

Which means you need to provide one more object to your call, or one less and use:

public static MvcHtmlString ActionLink(
    this HtmlHelper htmlHelper,
    string linkText,
    string actionName,
    string controllerName
)

So for example: @Html.ActionLink("Add Value", "AddValue", "DocGen") should do the job or:

@Html.ActionLink("Add Value", "AddValue", "DocGen", new{}, new { @class = "btn btn-primary" })

within the params object you'll probably want to pass some id or confirguration :)

4 Comments

@Desperado not really as you have overload of string, string, object, object, which is a perfect match for the arguments provided by OP, just that then "DocGen" is passed as object routeValues. It's easy to debug and check :)
so yeap, if it was you @Desperado that downvoted... then you're wrong
@mikus it's not me, please dont revenge on my post im not like that!
sure, I wouldn't :)

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.