【问题标题】:ASP.NET MVC trying to redirect to another page after submitASP.NET MVC 在提交后尝试重定向到另一个页面
【发布时间】:2017-05-11 18:35:28
【问题描述】:
在视图中,我有以下代码:-
@using(Html.BeginForm("addsurvey","survey")) {
当提交按钮被按下时,事件
[HttpPost]
public ActionResult addsurvey(Survey oSurvey)
{
被调用,但随后页面错误,因为它找不到 addsurvey.aspx 等页面。我没有错。表单所在的页面称为调查。为什么它不能只是刷新页面还是我需要
response.redirect("/survey");
【问题讨论】:
标签:
asp.net
asp.net-mvc
button
model-view-controller
submit
【解决方案1】:
您的方法必须返回ActionResult。通常当你使用return View() 时它是一个ViewResult,但是如果你想重定向到另一个动作,还有另一个返回类型。做吧:
return RedirectToAction("Index", "survey");
(只需将 Index 替换为目标操作)
【解决方案2】:
您可以在 addsurvey 操作方法中调用
View();(项目的View文件夹的controller文件夹中必须有addsurvey.cshtml)
View("actionName") ;(项目的View文件夹的controller文件夹中必须有actionName.cshtml) or
RedirectoAction("actionName","controllerName") (项目的View文件夹的controllerName文件夹下必须有actionName.cshtml)