3

This is a problem I haven't come across before.

I'm working on an MVC4 project. I'm using an asp button control because there isn't a Html Helper that can be used for a button (re: There's no @Html.Button !). My button code is:

<td><asp:Button ID="ButtonUndo" runat="server" Text="Undo" 
                        OnClick="ButtonUndo_Click" AutoPostBack="true"/></td>

I went to the Designer tab and clicked on this button which produced the event handler:

protected void ButtonUndo_Click(object sender, EventArgs e)
{
    RRSPSqlEntities db = new RRSPSqlEntities();
    int id = (int)ViewData["ClientId"];

    var updateAddress = (from a in db.Address
                             where a.PersonId == id
                             select a).SingleOrDefault();

    updateAddress.Deleted = false;
    db.SaveChanges();
}

I should add that this code was added to the same .aspx page wrapped in a script tag. Also within this section is the Page_Load method. The eventhandler is not within Page_Load.

The problem was found when I set a breakpoint and stepped through the code. Clicking my button shows that it doesn't hit my event handler at all. I don't know why this is, particularly as ASP created the event from clicking the button in Design mode.

Community
  • 1
  • 1
user2284341
  • 661
  • 4
  • 17
  • 38
  • ?? Why are you doing this? Why do you need the Button helper? Just but a submit button in the form and you have a working button. – Mike Cole May 16 '13 at 16:33
  • Why are you doing this? Lets start from the beginer! Asp.Net MVC != Asp.Net WebForms. There's no events! Its HTTP based Framework! Take a look here for help: http://www.asp.net/mvc – Fals May 16 '13 at 16:36

2 Answers2

5

Clicking my button shows that it doesn't hit my event handler at all.

This isn't all that surprising. ASP.NET MVC uses a completely different event model (i.e. it doesn't have one like web forms). However, what you're trying to do is very straight forward. In your controller build a new method, let's call it Undo:

public ActionResult Undo(int id)
{
    RRSPSqlEntities db = new RRSPSqlEntities();

    var updateAddress = (from a in db.Address
                             where a.PersonId == id
                             select a).SingleOrDefault();

    updateAddress.Deleted = false;
    db.SaveChanges();

    return View("{insert the original action name here}");
}

and then in your markup, simply markup the input like this:

<form method="POST" action="/ControllerName/Undo">
    @Html.HiddenFor(Model.Id)
    <input type="submit" value="Undo" />
</form>

where the Model for the View you're on contains a property, I've called it Id, that is the id you want passed into Undo.

Mike Perrenoud
  • 66,820
  • 29
  • 157
  • 232
0

I usually prefer to make ajax calls. You can try:

    <button type="button" class="button" onclick="ButtonUndo();" />

In the form:

    <script>
        function ButtonUndo() {
            $.ajax({
                    type: 'POST',
                    url: '/controller/action',
                    data: 'PersonID=' + ID,
                    dataType: 'json',
                    cache: false,
                    success: function (result) {
                        //do stuff here
                    },
                    error: function () {
                        //do error stuff here
                    }
            });
        }
    </script>

Controller:

    [HttpPost]
    public ActionResult Action(int PersonID)
    {
        //Do your stuff here

        return new JsonResult { result = "something" };
    }

(Sorry for any typos or syntax errors...I pulled from existing code that we use in a project.)

Amit M
  • 61
  • 7