C# 使用beginform过帐时MVC中未找到资源错误
以下代码未按预期正常工作 控制器有一个动作方法C# 使用beginform过帐时MVC中未找到资源错误,c#,asp.net-mvc,asp.net-mvc-4,asp.net-mvc-3,model-view-controller,C#,Asp.net Mvc,Asp.net Mvc 4,Asp.net Mvc 3,Model View Controller,以下代码未按预期正常工作 控制器有一个动作方法 [HttpPost] public ActionResult LoginResult() { string name = Request.Form["name"]; return View("Dashboard"); } public ActionResult Dashboard() { str
[HttpPost]
public ActionResult LoginResult()
{
string name = Request.Form["name"];
return View("Dashboard");
}
public ActionResult Dashboard()
{
string strName = Request["username"].ToString();
return View();
}
看来我有一个表格
@using (Html.BeginForm("LoginResult", "Dashboard", FormMethod.Post))
{
@Html.EditorFor(model =>model.username)
<button type="button" id="ajax_method">submit Via AJAX</button>
}
并再次修改了cshtml。但这也不起作用
@using (Html.BeginForm("login", "Dashboard", FormMethod.Post))
{
@Html.EditorFor(model =>model.username)
<button type="button" id="ajax_method">submit Via AJAX</button>
}
@使用(Html.BeginForm(“login”、“Dashboard”、FormMethod.Post))
{
@EditorFor(model=>model.username)
通过AJAX提交
}
这可能是一个愚蠢的错误,但它破坏了我的一天。谁能伸出手来解决这个问题呢
@using (Html.BeginForm("Action Name", "Controller Name", FormMethod.Post))
我认为您必须将“仪表板”更改为您的控制器名称在我的情况下,删除默认路由导致了我的问题
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { action = "Index", id = UrlParameter.Optional }
);
加上这个,我就解决了这个问题。我的项目是webforms和mvc的混合项目。我没有注意到。您的url不正确(您的方法是
LoginResult()
而不是Ajax()
)。然后它仍然会失败,因为您没有传递name
(只是AJAXParameter1
)的值,将其更改为url:“/Dashboard/LoginResult”,
-前导斜杠(或者更好的url:@url.Action(“LoginResult”,“Dashboard”)“,
感谢您指出错误。我已经更正了url问题。但它仍然重定向到另一个操作方法dashboard。令人困惑。DashboardController
中的LoginResult()
方法是吗?为什么在DashboardController
中有一个名为dashboard
的方法?(您是否添加了前导的/
?)是。DashboardController中的LoginResult方法。Dashboard将在页面加载时调用。
@using (Html.BeginForm("Action Name", "Controller Name", FormMethod.Post))
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { action = "Index", id = UrlParameter.Optional }
);