简体   繁体   English

如何将视图重定向到同一Controller上的另一个动作

[英]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: 通常,您需要执行以下两项操作:

  1. a GET action that shows the screen where the user will enter the values needed to create the new entity. GET操作,该屏幕显示用户将在其中输入创建新实体所需的值的屏幕。 This will have a form, which when submitted goes to the... 这将有一个表格,提交时将转到...
  2. POST action which accepts the form submission, checks the anti-forgery token, and does the actual data manipulation (adds the user into the database). POST操作,它接受表单提交,检查防伪令牌并进行实际的数据操作(将用户添加到数据库中)。 An additional best practice is to then Redirect to a GET action (eg RedirectToAction back to the Index action) to avoid double posting ( https://en.wikipedia.org/wiki/Post/Redirect/Get ) 另一种最佳实践是,然后重定向到GET操作(例如,将RedirectToAction返回到Index操作)以避免重复发布( https://en.wikipedia.org/wiki/Post/Redirect/Get

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.

相关问题 如何在同一控制器内的另一个动作中返回主视图? - How to return the main view in another action within same controller? 如何调用通过Javascript重定向到另一个操作的Controller Action? - How to call a Controller Action thats redirect to another action via Javascript? 如何从控制器重定向到另一个视图 - How to redirect to another view from controller 当两个控制器的动作名称相同时,从另一个控制器重定向到该控制器的动作 - Redirect to a controller's action from another controller while action name is same in both controllers 如何从ASP.NET MVC中的同一操作重定向到另一个视图或外部站点? - How can I redirect to either another view or an external site from the same action in ASP.NET MVC? 在MVC4中,如何使用Url中的参数从控制器动作重定向到视图? - In MVC4, how to redirect to a view from a controller action with parameters in the Url? 从控制器重定向到另一个控制器的另一个视图 - Redirect from controller to another view of another controller 从视图组件重定向到控制器操作 - redirect to controller action from view component 重定向到Sitecore中的相同控制器和操作方法 - redirect to the same controller and action method in sitecore 将变量从视图传递到 controller 操作方法,然后传递到同一 ASP.NET MVC controller 中的另一个方法 - Pass a variable from a view to a controller action method and then to another method in the same ASP.NET MVC controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM