【发布时间】:2019-04-26 04:47:48
【问题描述】:
我正在尝试重定向到另一个页面导致错误 404 无法解决
我的查看页面UserDashBoard.cshtml"有代码,
<fieldset >
<legend > User DashBoard </legend>
@if(Session["UserName"] != null)
{ <text >
Welcome @Session["UserName"].ToString() </text>
}
@using (Html.BeginForm("GetSurvey", "DashboardController"))
{
<input type="submit" value="Some text" />
}
</fieldset>
我想用控制器DashboardController.cs重定向到页面GetSurvey.cshtml
GetSurvey 就像,
@model List<SelectListItem>
@{
ViewBag.Title = "GetSurvey";
}
<h2>Survey List</h2>
<fieldset>
<legend> User DashBoard </legend>
@using (Html.BeginForm("GetSurvey", "DashboardController", FormMethod.Post))
{
@Html.DropDownList("ddlCustomers", Model)
<br />
<br />
<input type="submit" value="Submit" />
}
</fieldset>
而DashboardController 是,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace Survey.Controllers
{
public class DashboardController : Controller
{
//
// GET: /Dashboard/
public ActionResult GetSurvey()
{
List<SelectListItem> customerList = Survey();
return View(customerList);
// return View();
}
public ActionResult GetSurvey(string ddlCustomers)
{
List<SelectListItem> customerList = Survey();
if (!string.IsNullOrEmpty(ddlCustomers))
{
SelectListItem selectedItem = customerList.Find(p => p.Value == ddlCustomers);
ViewBag.Message = "Name: " + selectedItem.Text;
ViewBag.Message += "\\nID: " + selectedItem.Value;
}
return View(customerList);
}
private static List<SelectListItem> Survey()
{
SurveyAppEntities ObjectSur=new SurveyAppEntities();
List<SelectListItem> customerList = (from p in ObjectSur.Surveys.AsEnumerable()
select new SelectListItem
{
Text = p.Title,
Value = p.ID.ToString()
}).ToList();
return customerList;
}
}
}
我不知道为什么它没有重定向到上面提到的页面,因为我在启动项目时遇到了同样的问题,因为我重命名了 HomeController 名称。
路线:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Login", id = UrlParameter.Optional }
);
}
【问题讨论】:
-
这是您的路线,请发布您的路线
-
我已经用我的路线更新了帖子
-
您的表单使用
POST。你的控制器只处理GET。
标签: c# asp.net-mvc asp.net-mvc-4 asp.net-mvc-3 model-view-controller