Shax Shax - 2 months ago 16
ASP.NET (C#) Question

How to redirect a view to another action on the same Controller

I have a controller called StudentController, Index.cshtml and central layout.cschtml,

I am doing front end development after very long time and working on the frontend on asp.net mvc is quiet a challange for me now, so please forgive me if something is wrong.

What I am trying to achieve is that when I am running my application it runs perfectly find, but when I click the link Student it successfully goes to the Student Index view and hit the Index method in the StudentController.

But when I click on the Create New link on the Index view then it gives error (see at the bottom) , can someone guide what I am doing wrong....

StudentController

public StudentController(){}
public ActionResult Index()
{
var students =_studentDb.GetAll();
return View(students);
}

[HttpPost]
public ActionResult Create(Student student)
{
var result = _studentDb.Insert(student);
return View("Create");
}


_layout.chtml

<ul class="nav navbar-nav">
<li>@Html.ActionLink("Home", "Index", new { Controller = "Home", Area = "Common" })</li>
<li>@Html.ActionLink("Student", "Index", new { Controller = "Student", Area = "User" })</li>
<li>@Html.ActionLink("New Student", "Index", new { Controller = "CreateStudent", Area = "User" })</li>
</ul>


Index.chtml

@model IEnumerable<Student>

@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Index</h2>

<p>
@Html.ActionLink("Create New", "Create",new {Controller="Student",Area="User" })
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.FirstName)
</th>
<th>
@Html.DisplayNameFor(model => model.MiddleName)
</th>
<th>
@Html.DisplayNameFor(model => model.LastName)
</th>
<th>
@Html.DisplayNameFor(model => model.Gender)
</th>
<th>
@Html.DisplayNameFor(model => model.FirstNameAr)
</th>
<th>
@Html.DisplayNameFor(model => model.MiddleNameAr)
</th>
<th>
@Html.DisplayNameFor(model => model.LastNameAr)
</th>
<th></th>
</tr>

@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.FirstName)
</td>
<td>
@Html.DisplayFor(modelItem => item.MiddleName)
</td>
<td>
@Html.DisplayFor(modelItem => item.LastName)
</td>
<td>
@Html.DisplayFor(modelItem => item.FirstNameAr)
</td>
<td>
@Html.DisplayFor(modelItem => item.MiddleNameAr)
</td>
<td>
@Html.DisplayFor(modelItem => item.LastNameAr)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
@Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
@Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
</td>
</tr>
}

</table>


Error

Server Error in '/' Application.

The resource cannot be found.

Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.

Requested URL: /User/Student/Create

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.6.1055.0

Answer

First you need two methods on your controller, one for the GET and one for the POST. You only have the POST.

The GET will return the Create View that you have here, so get change the HttpPost attribute.

[HttpGet]
public ActionResult Create()
{
  var result = _studentDb.Insert(student);
  return View("Create");
}

If you want to Controller Action A to return Controller Action B, return RedirectToAction() instead of View()