Tuesday, 20 August 2013

How to navigate from one controller to another controller MVC

How to navigate from one controller to another controller MVC

i am trying to learn mvc. suppose i have two links in my index page. i
want that when i will click on first link then i want MyPageOneController
should invoke and MyPageOne view should display.
the same way i want that when i will click on second link then i want
MyPageTwoController should invoke and MyPagetwo view should display.
i try to do it but getting error. here i am giving the pic of my
controller & view folder structure. just have a look.

i have one view called MyPageOne which is in home folder and another view
in different folder called test.
here i am giving my full code
Index.cshtml code
@{
ViewBag.Title = "Home Page";
}
<h2>@ViewBag.Message</h2>
<p>
@Html.ActionLink("Click on link one", "MyPageOne",
"MyPageOne")&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
@Html.ActionLink("Click on link two", "MyPageTwo", "MyPageTwo")
</p>
MyPageOne.cshtml code
@{
ViewBag.Title = "MyPageOne";
}
<h2>Welcome to MyPageOne !</h2>
MyPageTwo.cshtml
@{
ViewBag.Title = "MyPageTwo.cshtml";
}
<h2>Welcome to MyPageTwo !</h2>
i have created two controllder called MyPageOneController.cs &
MyPageTwoController.cs
code in MyPageOneController.cs
namespace MvcApplication4.Controllers
{
public class MyPageOneController : Controller
{
//
// GET: /MyPageOne/
public ActionResult MyPageOne()
{
return View();
}
}
}
code in MyPageTwoController.cs
namespace MvcApplication4.Controllers
{
public class MyPageTwoController : Controller
{
//
// GET: /MyPageTwo/
public ActionResult MyPageTwo()
{
return View();
}
}
}
so just guide me what is wrong. i check that when i click on first link
then right controller method is getting called but no view is not getting
render. where i am making mistake. please guide me what i need to change
in my code to load two view created by me and stored in two different
folder. thanks

No comments:

Post a Comment