Disable deletion

Dec 31, 2009 at 7:18 PM

I would like to disable deletion on certain tables.

It would be nice to disable the edit/delete buttons from the grid and to disable deletion completely at the controller level.

I have been able to override the Edit method but I can't do so for the Delete one. I ma also looking for a way to show/hide the buttons in the grid.

Thanks,

 

Claude

Jan 3, 2010 at 2:45 PM

The deletion (and edit) links can easily be removed.  All you need to do is give them an empty string when you setup the grid object eg in this example you will get an edit link but no delete link


public class PersonGrid : GenericDataGrid<Person>

    {

        public PersonGrid()

            : base("list", "People", "/Person/GridData/", "Person/Edit/?", "", "id", "name"

            , new[]

                  {

                      new DataGridRow("id", "Id", 50),

                      new DataGridRow("name", "Name", 100)

                  })

        { }

    }

 

If you want other links you can of course just make a property on the base object and make a column from that.

Jan 4, 2010 at 5:01 PM
Edited Jan 4, 2010 at 6:28 PM

This works for the grid of course but I can still delete rows using the url.  Nevertheless, it doesn't impide using the urls.

I really need to override the delete or any other CRUD method that method that are not allowed.

 

Routing could be a solution. But, maybe not the right one.

e.g. /Products/delete/1 

 

 

I did override Create/Edit from the controller and assigned validation, but it wasn't successful for the Delete method. were I am receiving an error message : not all code paths return a value. 

        public override ActionResult Delete(RetiredProduct)
        {
             
       I got rid oft h}

i got rid of the error using this syntax but it not yet working completely.

public override ActionResult Delete(int id)

 

[AcceptVerbs(HttpVerbs.Post)]

        public override ActionResult Delete(RetiredProduct item){}

 

I got rid of the error using this syntax but it not yet working completely.

public override ActionResult Delete(int id)

 

I will look at your other post on model binding as it is a recommended way of doing it. But, for now, I would really like to find a quick fix.

Thanks for your help.

 

 

 

Jan 4, 2010 at 9:33 PM

Looks like this works.

 

        public override ActionResult Delete(int id)
        {
            try
            {
                TempData["Message"] = "Delete not allowed";
                return RedirectToAction("Index");
            }
            catch
            {
                TempData["Message"] = "An error has occured";
                return Index();
            }
        }

        public override ActionResult Delete(int id)

        {

            try

            {

                TempData["Message"] = "Delete not allowed";

                return RedirectToAction("Index");

            }

            catch

            {

                TempData["Message"] = "An error has occured";

               return Index();

            }

        }

 

Jan 5, 2010 at 6:35 AM

Yeah overriding the delete and returning another ActionResult is the way to do it.  Probably a redirect as you have it in your try block, the catch should not the necessary.

You can also use the [HandleErrors] attribute to do nice redirects to error pages etc.

I think using the automated model binder will be your easiest solution, if you have any questions let me know.

Jan 7, 2010 at 3:24 PM

Thanks for your good advises. I can afford some coaching! 

I don't get the model binder concept very well yet. I will need to dig it a bit.

For now I am fighting to make a Multiselect Listbox Helper going.