0

Issue


I have been given a piece of code that was written years ago by someone else and I am trying to get my head around why this piece of code is not working.

I have a Edit.aspx page which inside has a list of hyperlinks and one of these links is a Delete label, but has a RenderPartial in it's place (See Below):

<% Html.RenderPartial("UserDeleteLink", Model); %>

Inside the UserDeleteLink.ascx page it contains the delete button:

<li>
    <% using (Html.BeginForm("Delete", "User", new { id = Model.uID }, FormMethod.Post, new { id = "DeleteForm" }))

    { %> 
        <a class="delete-user-button expired" href="javascript:void(0);"><%: Resources.GeneralDelete %></a>         
 <% } %>
</li>

Now inside the UserController.cs it has a method which deletes the record from the database:

[HttpPost, ActionName("Delete")]
[RequireAdminAttribute]
[Authorize]
public ActionResult DeleteConfirmed(int id)
{
    _userRepository.Delete(id);
    _userRepository.Save();
    return RedirectToAction("Index", "Admin");
}

The problem is that the method is not getting hit, and I can't seem to debug the issue.

Can anyone see the issue?

11
  • Is a different action being invoked? What is the request to the server and what is the server's response? Commented Jan 16, 2017 at 15:42
  • Is the account you are logged in as Authorised? Based off the Authorise tag and AdminAttribute Commented Jan 16, 2017 at 15:44
  • 1
    What delete button? That's an empty hyperlink, with something very weird in the href. Commented Jan 16, 2017 at 15:44
  • @buffjape <%: Resources.GeneralDelete %> is "Delete" just in a Resource file. Commented Jan 16, 2017 at 15:45
  • @David Where can I track the request for the server? Commented Jan 16, 2017 at 15:47

3 Answers 3

1

There might be 3 things going on here...

  1. Looks like you are not submitting. Try adding a submit button inside your form:

    <input type='submit' value='Delete' />
    
  2. Check your id param. In your controller, it is an int. But in your form it looks like you are assigning a string. The model binder could be returning an error.

  3. Check your routing config (usually in App_Start). Check to see if it is using the default {controller}/{action}, or if there is some custom routing going on.

And finally, try using F12 debugging tools when submitting the form. Check the network traffic to see exactly what url is being hit, and what parameters are being sent. You can also see some error messages coming back from your POST.

Sign up to request clarification or add additional context in comments.

2 Comments

Just added this and it did not trigger the method.
Check your routing configuration. Is the app using the default {controller}/{action} config, or is there some custom routing going on? You also might check the F12 debugging options to see exactly what url is being hit, and if there is any error message (501, 401, etc) coming back.
0

Seems like you don't submit the form.

Try adding onclick="form.submit();" to your link element

<a class="delete-user-button expired" href="javascript:void(0);" onclick="form.submit();"><%: Resources.GeneralDelete %></a>

2 Comments

Just added this and the method did not trigger.
How is the generated html looking?
0

Try the below, Remove all attributes from the Action method and simplify the form to the following

//[HttpPost, ActionName("Delete")]
//[RequireAdminAttribute]
//[Authorize]
public ActionResult DeleteConfirmed(int id)
{
    _userRepository.Delete(id);
    _userRepository.Save();
    return RedirectToAction("Index", "Admin");
}

<% using (Html.BeginForm("DeleteConfirmed", "User"))
{ %>
<input name="id" value="1"/>
<input type='submit'/>
<% } %>

or better still, use the HTML form tags to create the form.

Once you get the basic thing working, try one by one to add back the attributes to the ActionMethod and the Form. That should help you pin point why it does not work.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.