如何将视图重定向到同一控制器上的另一个操作

本文关键字:控制器 另一个 操作 视图 重定向 | 更新日期: 2023-09-27 18:11:43

我有一个名为StudentController, Index的控制器。CSHTML和中央布局。cschtml,

我在做前端开发很长一段时间后,在asp.net mvc前端工作对我来说是一个挑战,所以如果有什么问题,请原谅我。

我想要实现的是,当我运行我的应用程序时,它会完美地找到,但是当我单击链接Student时,它成功地转到Student Index视图并在StudentController中命中Index方法。

但是当我点击索引视图上的创建新链接,然后它给出了错误(见底部),有人可以指导我做错了什么....

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>
误差

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

如何将视图重定向到同一控制器上的另一个操作

你得到一个错误,因为你正在生成一个<a>标签的post动作。您只能通过javascript中的表单post或ajax访问post操作。如果您试图像这样链接到另一个视图,则需要删除[HttpPost]属性。此外,似乎你想要传递你的result到你的创建视图,如:return View(result)(使用这个重载,因为你的视图有相同的名称作为你的动作)。此外,您的Create操作有一个Student参数,但您没有向其传递值。我建议使用如下结构:

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
}

然后你的Create视图应该包含一个表单,用于输入关于学生的数据,这些数据将发布到/User/student/Create。

像这样修改代码

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

首先在控制器上需要两个方法,一个用于GET,一个用于POST。你只有POST。

GET将返回你在这里的Create View,所以GET更改HttpPost属性。

[HttpGet]
public ActionResult Create()
{
  return View("Create");
}

如果你想让Controller Action A返回Controller Action B,返回RedirectToAction()而不是View()

一般需要两个动作:

  1. 一个GET动作,显示用户将在其中输入的屏幕创建新实体所需的值。它会有一个形式,当提交到…
  2. POST操作接受表单提交,检查防伪令牌,并进行实际的数据操作(添加用户进入数据库)。另一个最佳实践是重定向到GET操作(例如,重定向到索引)(行动),以避免重复投寄(https://en.wikipedia.org/wiki/Post/Redirect/Get)

你只有POST动作…你不能链接到那个,你需要以表单的形式提交。因此,您还需要创建GET操作