1

I have a View which is accesible http://localhost:49467/About/ and its called index.cshtml.

I have another view in which is accessible by http://localhost:49467/Food/ and is called index2.cshtml

I want to place a link inside the index.cshtml page so i could access index2.cshtml. How can i do it. My code;

@Html.ActionLink("link", "Food/index" ) 

But i end up in the URL http://localhost:49467/About/Food/ and the page is not found. All what i want to do is to navigate to http://localhost:49467/Food/ instead. How can i correct this ?

3
  • @Html.ActionLink("link", "Food/index2") ? Commented Feb 21, 2013 at 19:50
  • Doesn't work. goes to ../About/Food/index2 instead Commented Feb 21, 2013 at 19:53
  • Why are you using "Food/Index2"? Use the method properly... @Html.ActionLink("Link Text","Index","Food") Commented Feb 21, 2013 at 19:57

2 Answers 2

3

'The ActionLink helper is calling a controller action, not a static URL

@HTML.ActionLink("linktext", "ControllerName")

Is your Index2.cshtml associated with the Food Contoller's Index action? If so you can do this:

@Html.ActionLink("Linktext", "Index2", "Food", null, null)

The third parameter above is the name of the controller for the associated action.

If index.cshtml is also associated with the food controller you don't need to specify the controller name:

@Html.ActionLink("Linktext", "Index2")
Sign up to request clarification or add additional context in comments.

Comments

1
@Html.ActionLink("link", "/Food/index" ) 

note the extra slash. Or it seems like your Routes dictate that /Food uses index2.cshtml. so you should be able to get away with

@Html.ActionLink("link", "index2", "Food" ) 

With MVC you worry less about the file names of the views but through which controllers and actions you need to go through to get to that view.

3 Comments

No but, it goes to ../About/Food/index and not to ../Food/index2
do you have a FoodController? And my apologies, this was off the top of my head, pulled up my mvc project to verify the edit.
Yes, i have a food controller.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.