简体   繁体   中英

Can not call action from another controller MVC 4

So i have 2 controller in my controller folder, BookController and HomeController. I am trying to make a header and call it with partial call so i don't have to create it multiple time.

I made my header like this

<div id ="header">
<img src="Images/logo.png" id="logo" />
<div class="navbar">@Html.ActionLink("Home","index")</div>
<div class="navbar">@Html.ActionLink("Loan","index","LoanController")</div>
<div class="navbar">Student</div>
<div class="navbar">Book</div>

when i am click it home it can bring me to the main page, but when i am click loan i got error "Server Error in '/' Application". I have try to change the loan controller with "~/LoanController" and "~/Controller/LoanController" but i keep got the same error. Can somebody help me solve this?

You don't need the "Controller" part of the Loan. ASP.NET MVC does that for you.

<div class="navbar">@Html.ActionLink("Loan","index","Loan")</div>

Edit: Behind the scenes MVC will look inside your Controllers Folder (which is the default folder for controllers) to a file that starts with "Loan".

In your case, you are using this overload of ActionLink :

LinkExtensions.ActionLink Method (HtmlHelper, String, String, String)

In which takes a controller name as the last parameter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM