我正在尝试在我的程序中创建删除功能。我按照this教程。但当我尝试删除此错误时,就出现了。
这是我从删除模型中的类。
public bool deleteAccount(int Id) {
using (SqlConnection conn = new SqlConnection(connectionString))
using (SqlCommand comObj = new SqlCommand("", conn)) {
comObj.CommandText = "DELETE FROM account WHERE userId = @userId";
comObj.Parameters.AddWithValue("@userId", Id);
conn.Open();
int res = comObj.ExecuteNonQuery();
if (res >= 1)
{
return true;
}
else
{
return false;
}
}
}
我的删除操作控制器
public ActionResult deleteAccount()
{
return View();
}
public ActionResult deleteAccount(int id)
{
try
{
var databaseModel = new database();
if (databaseModel.deleteAccount(id)) {
ViewBag.AlertMsg = "Employee details deleted successfully";
}
return RedirectToAction("GetAllEmpDetails");
}
catch {
return View();
}
完整堆栈跟踪
[AmbiguousMatchException: The current request for action 'deleteAccount' on controller type 'accountController' is ambiguous between the following action methods:
System.Web.Mvc.ActionResult deleteAccount() on type directory.Controllers.accountController
System.Web.Mvc.ActionResult deleteAccount(Int32) on type directory.Controllers.accountController]
System.Web.Mvc.ActionMethodSelectorBase.FindActionMethod(ControllerContext controllerContext, String actionName) +113
System.Web.Mvc.Async.ReflectedAsyncControllerDescriptor.FindAction(ControllerContext controllerContext, String actionName) +54
System.Web.Mvc.ControllerActionInvoker.FindAction(ControllerContext controllerContext, ControllerDescriptor controllerDescriptor, String actionName) +203
System.Web.Mvc.Async.AsyncControllerActionInvoker.BeginInvokeAction(ControllerContext controllerContext, String actionName, AsyncCallback callback, Object state) +136
System.Web.Mvc.Controller.<BeginExecuteCore>b__1c(AsyncCallback asyncCallback, Object asyncState, ExecuteCoreState innerState) +25
System.Web.Mvc.Async.WrappedAsyncVoid
1.CallBeginDelegate(AsyncCallback callback,Object callbackState)+30
System.Web.Mvc.Async.WrappedAsyncResultBase 1.Begin(AsyncCallback callback, Object state, Int32 timeout) +128
System.Web.Mvc.Controller.BeginExecuteCore(AsyncCallback callback, Object state) +465
System.Web.Mvc.Controller.<BeginExecute>b__14(AsyncCallback asyncCallback, Object callbackState, Controller controller) +18
System.Web.Mvc.Async.WrappedAsyncVoid
1.CallBeginDelegate(AsyncCallback callback,Object callbackState)+20
System.Web.Mvc.Async.WrappedAsyncResultBase 1.Begin(AsyncCallback callback, Object state, Int32 timeout) +128
System.Web.Mvc.Controller.BeginExecute(RequestContext requestContext, AsyncCallback callback, Object state) +374
System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.BeginExecute(RequestContext requestContext, AsyncCallback callback, Object state) +16
System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__4(AsyncCallback asyncCallback, Object asyncState, ProcessRequestState innerState) +52
System.Web.Mvc.Async.WrappedAsyncVoid
1.CallBeginDelegate(AsyncCallback callback,Object callbackState)+30
System.Web.Mvc.Async.WrappedAsyncResultBase 1.Begin(AsyncCallback callback, Object state, Int32 timeout) +128
System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state) +384
System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state) +48
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData) +16
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +103
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155
希望有人可以帮助我。
|编辑|
我刚才尝试了其他教程。我也尝试添加[HttpPost],但传递给URL的ID始终为0.所以我尝试显示我的userId
acc.userName = rdr["userId"].ToString();
我获得的每个ID都是0
答案 0 :(得分:2)
请查看以下资源,希望它有所帮助,我认为您应该开始查看实体框架以进行数据访问。
您可以执行此示例中的操作
// GET: /Movies/Delete/5
public ActionResult Delete(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Movie movie = db.Movies.Find(id);
if (movie == null)
{
return HttpNotFound();
}
return View(movie);
}
// POST: /Movies/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(int id)
{
Movie movie = db.Movies.Find(id);
db.Movies.Remove(movie);
db.SaveChanges();
return RedirectToAction("Index");
}
答案 1 :(得分:0)
第二个控制器必须具有以下请求方法:
public ActionResult deleteAccount()
{
return View();
}
[HttpPost]
public ActionResult deleteAccount(int id)
{
try
{
var databaseModel = new database();
if (databaseModel.deleteAccount(id))
{
ViewBag.AlertMsg = "Employee details deleted successfully";
}
return RedirectToAction("GetAllEmpDetails");
}
catch
{
return View();
}
}
答案 2 :(得分:0)
默认情况下,如果我们没有明确指定获取或发布,则操作方法为获取,因此当您尝试调用一个最终混淆了哪个操作方法应该调用(带参数或没有参数),你需要指定哪个用于获取哪个用于发布像:
[HttpGet]
public ActionResult deleteAccount()
{
return View();
}
[HttpPost]
public ActionResult deleteAccount(int id)
{
try
{
var databaseModel = new database();
if (databaseModel.deleteAccount(id)) {
ViewBag.AlertMsg = "Employee details deleted successfully";
}
return RedirectToAction("GetAllEmpDetails");
}
catch {
return View();
}
}
希望它有所帮助!
答案 3 :(得分:0)
对于相同的操作名称,您不能使用相同的HTTP谓词。换句话说,让HttpGet用于相同的动作名称,即使是过载,也是不可能的。
将一个或您的操作方法更改为其他HTTP操作动词...
[HttpGet]
public ActionResult deleteAccount()
{
//..
}
[HttpPost]
public ActionResult deleteAccount(string country)
{
//..
}
答案 4 :(得分:0)
如果您使用的是WebAPI2,则可以指定路由前缀并将控制器名称设置为“删除”(不含DeleteXXX)
例如,
[RoutePrefix("api/Account")]
public class AccountController : ApiController
{
public IHttpActionResult Delete(Guid id)
{
}
public IHttpActionResult Delete()
{
}
}