Kanwar Singh
Kanwar Singh

Reputation: 908

how to delete data from in mvc3

I want to delete the data. here is my code. when i trying to delete the data it gives an error.

this is my code

  public ActionResult delete(Int32 id)
    {
        var contentdelete = (from m in _db.tb_content
                             where m.id == id
                             select m).First();
        return View(contentdelete);
    }
    public ActionResult delete(MvcNTL.Models.tb_content contentdelete)
    {
        var content = (from m in _db.tb_content
                       where m.id == contentdelete.id
                       select m).First();
        if (!ModelState.IsValid)
            return View(content);
        _db.ApplyCurrentValues(content.EntityKey.EntitySetName, contentdelete);
        _db.SaveChanges();
        return RedirectToAction("index");
    }

this is the error

The current request for action 'delete' on controller type 'ContentController' is ambiguous between the following action methods: System.Web.Mvc.ActionResult delete(Int32) on type MvcNTL.Controllers.ContentController System.Web.Mvc.ActionResult delete(MvcNTL.Models.tb_content) on type MvcNTL.Controllers.ContentController

Upvotes: 1

Views: 346

Answers (3)

Kanwar Singh
Kanwar Singh

Reputation: 908

 [HttpPost]
 public ActionResult delete(MvcNTL.Models.tb_content contentdelete)
 {
var content = (from m in _db.tb_content
               where m.id == contentdelete.id
               select m).First();
if (!ModelState.IsValid)
    return View(content);
_db.ApplyCurrentValues(content.EntityKey.EntitySetName, contentdelete);
_db.SaveChanges();
return RedirectToAction("index");
}

Upvotes: 0

Darin Dimitrov
Darin Dimitrov

Reputation: 1039508

You cannot have 2 actions with the same name on the same controller accessible with the same HTTP verb. You should decorate the second one with the [HttpPost] attribute:

[HttpPost]
public ActionResult delete(MvcNTL.Models.tb_content contentdelete)
{
    var content = (from m in _db.tb_content
                   where m.id == contentdelete.id
                   select m).First();
    if (!ModelState.IsValid)
        return View(content);
    _db.ApplyCurrentValues(content.EntityKey.EntitySetName, contentdelete);
    _db.SaveChanges();
    return RedirectToAction("index");
}

This makes the second action that is actually performing the delete accessible only with the POST verb. The first action will be accessible with the GET verb and would render the form.

Upvotes: 1

Jamie
Jamie

Reputation: 3071

You need to add [HTTPPOST] At your second delete controller. They both are get controllers now, so mvc doesn't know which one to pick.

Upvotes: 1

Related Questions