[英]How to redirect a view to another action on the same Controller
I have a controller called StudentController, Index.cshtml and central layout.cschtml, 我有一个名为StudentController,Index.cshtml和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. 很长时间之后,我就开始进行前端开发,而在asp.net mvc上的前端工作现在对我来说是一个挑战,因此,如果出现问题,请原谅我。
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. 我想要实现的是,当我运行我的应用程序时,它可以完美运行,但是当我单击链接Student时,它成功进入了Student Index视图,并在StudentController中命中了Index方法。
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 _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 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
You're getting an error because you're generating an <a>
tag to a post action. 之所以出错,是因为您正在为发布操作生成
<a>
标记。 You can only access post actions through a form post or ajax in javascript. 您只能通过javascript中的表单发布或ajax访问发布操作。 If you're trying to link to another view like that, you'll want to remove the
[HttpPost]
attribute. 如果您试图链接到另一个这样的视图,则需要删除
[HttpPost]
属性。 Additionally, it seems that you'd want to pass your result
into your Create view like: return View(result)
(using this overload because your view has the same name as your action). 另外,似乎您希望将
result
传递到Create视图中,例如: return View(result)
(使用此重载,因为您的视图与操作具有相同的名称)。 Additionally, your Create action has a Student parameter, but you aren't passing values into it. 此外,您的“创建”操作具有一个“学生”参数,但您没有在其中传递值。 I'd recommend a structure more like the following:
我建议一种类似于以下的结构:
public IActionResult Create()
{
var student = new Student();
return View(student)
}
[HttpPost]
public IActionResult Create(Student student)
{
_studentDb.Add(student);
_studentDb.SaveChanges();
// Do whatever you like to finish this action
}
And then your Create view should contain a form for entering data about the student that would post to /User/Student/Create. 然后,您的“创建”视图应包含一个表单,用于输入要发布到/ User / Student / Create的有关学生的数据。
change code like this 像这样更改代码
public ActionResult Create()
{ return View(); }
[HttpPost]
public ActionResult Create(Student student)
{
var result = _studentDb.Insert(student);
return View();
}
First you need two methods on your controller, one for the GET and one for the POST. 首先,您需要在控制器上使用两种方法,一种用于GET,另一种用于POST。 You only have the POST.
您只有POST。
The GET will return the Create View that you have here, so get change the HttpPost attribute. GET将返回您在此处拥有的Create View,因此请更改HttpPost属性。
[HttpGet]
public ActionResult Create()
{
return View("Create");
}
If you want to Controller Action A to return Controller Action B, return RedirectToAction() instead of View() 如果要让控制器动作A返回控制器动作B,请返回RedirectToAction()而不是View()
Generally you need two actions: 通常,您需要执行以下两项操作:
You only have the POST action...you can't link to that, you need to submit it as a form. 您只有POST操作...您无法链接到该操作,您需要将其提交为表单。 So you need to create the GET action as well.
因此,您还需要创建GET操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.