Asp.net mvc 找不到我刚刚创建的ActionMethod
因此,我创建了一个表来保存所有经过身份验证的用户的额外信息。此表还链接到my db中的许多其他表。此表通过用户id连接到asp.net标识,尽管有多个字段与成员表(电子邮件和用户名)共享相同的信息。不幸的是,有一个bug删除了我添加的users表中的一些成员数据,而不是identity表本身。这个bug本身已经被修复,但是我正在尝试创建一种方法来从成员表中检索丢失的信息。我这样做的方式是在用户的编辑屏幕上添加一个按钮(不是usersadmin页面,而是我添加的users表)。将我带到操作的按钮的代码如下所示: 操作按钮Asp.net mvc 找不到我刚刚创建的ActionMethod,asp.net-mvc,routing,Asp.net Mvc,Routing,因此,我创建了一个表来保存所有经过身份验证的用户的额外信息。此表还链接到my db中的许多其他表。此表通过用户id连接到asp.net标识,尽管有多个字段与成员表(电子邮件和用户名)共享相同的信息。不幸的是,有一个bug删除了我添加的users表中的一些成员数据,而不是identity表本身。这个bug本身已经被修复,但是我正在尝试创建一种方法来从成员表中检索丢失的信息。我这样做的方式是在用户的编辑屏幕上添加一个按钮(不是usersadmin页面,而是我添加的users表)。将我带到操作的按钮的
UserReset操作代码如下所示: 用户重置操作代码
我目前遇到的问题实际上是无法调用此操作(甚至在此时打开编辑页面)。每次我尝试加载页面时,它都会抛出一个“控制器中未找到公共操作方法”错误。我觉得这对我来说是一个新手的错误,但有人能告诉我我做错了什么吗?我将对幕后故事保持沉默,只回答以下问题: 我发现了两件重要的事情。第一个是CSHTML(但不是当前特定错误的直接原因)。请参阅下面的CSHTML建议(特别是在C#Action修复后遇到更多问题时) 首先,你的控制器。如果查看您的
UserReset
操作,您会注意到您用[HttpPost]
装饰了它。正如您所说,您无法打开编辑页面。这是因为编辑页面操作不存在(例如,请求Url处的[HttpGet]
操作)。这就是您需要的:
public class TSTUsersController : IController
{
...
//You need this action to process the get request
[HttpGet]
public ActionResult UserReset()
{
return View("UserReset"); //return the edit form html to the user
}
//this method will handle the button click
[HttpPost]
public ActionResult UserReset( String email )
{
if(ModelState.IsValid)
{
//save the information to the database
//direct the user to some sort of confirmation page
return RedirectToAction("Index", "Home");
}
//return the form with the error messages
return View("UserReset", email);
}
...
}
据我所知,您完全误解了HTML表单提交
元素缺少两个主要参数:
<form
method="POST"
action="@Url.Action("UserReset", 'TSTUsers")" //e.g. POST /TSTUsers/UserReset
... >
...
<button>Submit</button>
</form>
或者,使用帮助程序:
@using( Html.BeginForm( "UserReset", "TSTUsers", FormMethod.Post ) )
{
<button>Submit</button>
}
@using( Html.BeginForm( "UserReset", "TSTUsers", FormMethod.Post, new { email = Model.Email } ) )
{
<button>Submit</button>
}
@using( Html.BeginForm( "UserReset", "TSTUsers", FormMethod.Post ) )
{
@Html.Hidden("email", Model.Email )
<button>Submit</button>
}
或者,使用帮助程序:
@using( Html.BeginForm( "UserReset", "TSTUsers", FormMethod.Post ) )
{
<button>Submit</button>
}
@using( Html.BeginForm( "UserReset", "TSTUsers", FormMethod.Post, new { email = Model.Email } ) )
{
<button>Submit</button>
}
@using( Html.BeginForm( "UserReset", "TSTUsers", FormMethod.Post ) )
{
@Html.Hidden("email", Model.Email )
<button>Submit</button>
}
@使用(Html.BeginForm(“UserReset”、“TSTUsers”、FormMethod.Post))
{
@Html.Hidden(“email”,Model.email)
提交
}
注意:该按钮与编辑页面的其余部分一样位于单独的html.beginform中。我不知道这是否有用,但在我看来这是问题的一部分