Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/261.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用beginform过帐时MVC中未找到资源错误_C#_Asp.net Mvc_Asp.net Mvc 4_Asp.net Mvc 3_Model View Controller - Fatal编程技术网

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 }
            );