tags:

views:

109

answers:

3

Ok i have an mvc app. and im trying to get my delete to work. Basically i want it so when i click delete it takes me to a page saying "are you sure?" i have that working, the problem is catching the request and actually doing the delete. i tried diffrent methods. as below.

 public ActionResult Delete(int id)
    {
        var something = friendsDB.Friends.Single(a => a.Id == id);
        return View(something);
    }
    [HttpPost]
    public ActionResult Delete(int id, string confirmButton)
    {
        var something = friendsDB.Friends.Single(a => a.Id == id);

        // For simplicity, we're allowing deleting of albums
        // with existing orders We've set up OnDelete = Cascade
        // on the Album->OrderDetails and Album->Carts relationships

        friendsDB.DeleteObject(something);
        friendsDB.SaveChanges();

        return View("Index");
    }

That doesnt work cause, deleteobject and savechanges claim

"C:\Users\Mtszc\Documents\Visual Studio 2010\Projects\Test\Test\Content\Controllers\DownloadsController.cs(36,23): error CS1061: 'Test.Models.FriendsDB' does not contain a definition for 'DeleteObject' and no extension method 'DeleteObject' accepting a first argument of type 'Test.Models.FriendsDB' could be found (are you missing a using directive or an assembly reference?)"

the second thing i tried was

public ActionResult Delete(int id)
    {
        var something = friendsDB.Friends.Single(a => a.Id == id);
        return View(something);
    }
    [HttpDelete]
    public ActionResult Delete(Friend myFriend)
    {
        try
        {
            friendsDB.Friends.DeleteOnSubmit(myFriend);
            friendsDB.SubmitChanges();
            return RedirectToAction("Index");
        }
        catch
        {
            return View();
        }
    }

that didnt work. it compiled but when i click delete, and it takes me to the page where i say im sure i want to delete, it returns view, which was the catch, meaning the try failed.

this is a simple sql database i made, Id, name, link. and im using linq to sql class. i c can create and view, but not delete.

A: 

The error messages appear to be confusing you.

"C:\Users\Mtszc\Documents\Visual Studio 2010
\Projects\Test\Test\Content\Controllers\DownloadsController.cs(36,23): error 
CS1061: 'Test.Models.FriendsDB' does not contain a definition for 'DeleteObject'
and no extension method 'DeleteObject' accepting a first argument of
type 'Test.Models.FriendsDB' could be found (are you missing a using directive
or an assembly reference?)"

Is not referring to the MVC Action, it is referring to your Test.Models.FriendsDB method call:

friendsDB.DeleteObject(something);   

It sounds like you have not Defined the method "DeleteObject" on your friendsDB model or you do not have a overloaded method that accepts a 'Test.Models.FriendsDB' object type.

Secondly, don't confuse HTTP Methods (Get, Post, Put, Delete) with what you are trying to accomplish. "Put" and "Delete" are methods I don't believe web browsers use often if at all. Most requests are GET unless you are submitting a form, then they are POST. Adding the HttpDelete will most likely render that Action useless. If you only want a Delete action from a form submit then add HttpPost

Erik Philips
I don't have enough reputation to edit but in your post above you said most requests are GET unless submitting a form then they are PUT. That should say that if they are submitting a form they are POST.
Mike
yes i am aware that mostly supported http requests are post and get, i was just trying my luck. can someone help me define a delete method in my model class.
Matiszac
Thanks Mike! (I knew that seriously... lol)
Erik Philips
+1  A: 

Try something like this instead.

var rowToDelete = friendsDB.Friends.Single(a => a.ID == myFriend.ID);
friendsDB.Friends.DeleteOnSubmit(rowToDelete);
friendsDB.SubmitChanges();

That would be a simple way of taking care of the record delete with Linq. I apologize if the syntax isn't perfect since I'm writing it on the fly out of my head.

By the way there are some GREAT videos made by the guys over at Microsoft for learning ASP.NET MVC as well as LINQ. Check these out.

http://www.asp.net/mvc

Cheers

Mike
YEH THANKS LOL you posted that right after i figured it out. but thanks man im glad to see its correct !!
Matiszac
don't forget to mark your questions as resolved.
Mike
A: 

Ok for who ever views this i solved the problem. Through hours of digging, i solved the problem. For anyone who made a sql data base and made a model class for it using linq to sql this is how to get delete to work.

public ActionResult Delete(int id)
    {
        var something = friendsDB.Friends.Single(a => a.Id == id);
        return View(something);
    }
    [HttpPost]
    public ActionResult Delete(int id, string confirmButton)
    {
        var sigh = friendsDB.Friends.Single(a => a.Id == id);
        try
        {
            friendsDB.Friends.DeleteOnSubmit(sigh);
            friendsDB.SubmitChanges();
            return RedirectToAction("Index");
        }
        catch
        {
            return View();
        }
    }

with this, create a strongly typed delete view.

Matiszac