Multiple actions on one page

Last post 01-25-2008 4:57 PM by tgmdbm. 7 replies.

Sort Posts:

  • Multiple actions on one page

    01-15-2008, 11:21 AM
    • Member
      417 point Member
    • andymac7
    • Member since 03-12-2003, 2:51 AM
    • Glasgow, Scotland
    • Posts 106

    Hi all,

    I'm just getting started with the MVC framework and I'm trying to add a number of actions onto a single aspx page. Basically the page contains a list of cities and each item in the list has a delete and an update icon beside it. The page also contains a form for adding a new city which is blank when the page is loaded but when the update option is chosen it will be populated with the relevant form data.Has anyone come across a good way of doing something like this?

    I've got the individual components/actions working on their own it's just adding it all onto the one page that im struggling with.

    Thanks in advance,
    Andy
  • Re: Multiple actions on one page

    01-15-2008, 12:39 PM
    Answer
    • Member
      574 point Member
    • abombss
    • Member since 06-27-2006, 4:13 PM
    • Chicago, IL
    • Posts 164

    Use ajax to make the updates and deletes.  Without ajax each delete button should be in its own form with the form's action pointing to the controller action and id of the item.  JQuery would be my library of choice and it should be relatively painless.

     

    <tr id="1">
    <td>
    <a href="/customers/1/delete" onclick="deleteCustomer(1);return false;">Delete</a>
    </td>
    <td>
    <a href="/customers/1/edit" onclick="editCustomer(1);return false;">Update</a>
    </td>
    <td class="first-name">Adam</td>
    </tr>

    <div id="edit-form" style="display:none">
    <form id="customer-edit-form" action="" method="post" onsubmit="updateCustomer(); return false;">
    <label for="customer_firstName">First name:</label>
    <input id="customer_firstName" name="customer.firstName" value="" />
    <input type="submit" />
    </form>
    </div>

    <script type="text/javascript">
    function deleteCustomer(id) {
    $.ajax({type:"DELETE", url: "/customers/" + id, success:function(){$("#" + id).remove();});
    }

    function editCustomer(id) {
    var row = $("#" + id);
    $("customer-edit-form").attr("action", "/customers/" + id);
    $("#customer_firstName").value = row.children(.first-name :first).val();
    $("#edit-form").show();
    }

    function updateCustomer() {
    var f = $("customer-edit-form");
    $.post(f.attr("action"), f.serialize());
    }
    </script>
      
    Adam Tybor -- abombss.com
  • Re: Multiple actions on one page

    01-15-2008, 3:47 PM
    • Member
      592 point Member
    • MikeBosch
    • Member since 02-13-2007, 3:17 AM
    • Miami, FL
    • Posts 127

    That's awesome... How do you update the page after the ajax call? 

    “It is not the strongest of the species that survives, nor the most intelligent. It is the one that is the most adaptable to change.”

    http://weblogs.asp.net/mikebosch

    *** Please MARK this post as ANSWERED, if you find it helpful) ***
  • Re: Multiple actions on one page

    01-15-2008, 4:01 PM
    • Member
      574 point Member
    • abombss
    • Member since 06-27-2006, 4:13 PM
    • Chicago, IL
    • Posts 164

     The JQuery docs are really good.  They have callbacks for onSuccess and onError that you can use.  In your controller you would detect that it was an xhr request.  Google around I forget what headers are sent, then render a partial view instead of a full view.  You can then do what ever you need to in the onSuccess.  The delete action uses an http request of type DELETE, unless you are using simplyrestful routing this probably not what you want.  I just threw the fake code up there to give you an idea of what you can do.

    Adam Tybor -- abombss.com
  • Re: Multiple actions on one page

    01-16-2008, 12:39 PM
    • Member
      417 point Member
    • andymac7
    • Member since 03-12-2003, 2:51 AM
    • Glasgow, Scotland
    • Posts 106

    Abomss thats great thanks for the reply.

  • Re: Multiple actions on one page

    01-25-2008, 7:14 AM
    • Member
      28 point Member
    • byron.walker
    • Member since 01-11-2008, 1:55 AM
    • Posts 14

    This solution has involves a destructive GET being the delete action.

     

    A crawler or a link checking tool will destroy data in this case and really should be avoided. 

  • Re: Multiple actions on one page

    01-25-2008, 9:55 AM
    • Member
      592 point Member
    • MikeBosch
    • Member since 02-13-2007, 3:17 AM
    • Miami, FL
    • Posts 127

    Does this still apply even though its executing through js?  Hopefully a crawler wouldn't get this deep into an application...

    “It is not the strongest of the species that survives, nor the most intelligent. It is the one that is the most adaptable to change.”

    http://weblogs.asp.net/mikebosch

    *** Please MARK this post as ANSWERED, if you find it helpful) ***
  • Re: Multiple actions on one page

    01-25-2008, 4:57 PM
    • Contributor
      4,287 point Contributor
    • tgmdbm
    • Member since 12-17-2007, 9:08 AM
    • Posts 872
    • ASPInsiders
      TrustedFriends-MVPs

    Certain crawlers may execute js but the vast majority don't. they typically don't post forms either, just follow links.

    I would rewrite it so that the delete function POSTs instead of GETs. in fact i always require that the delete actions be called via a POST such that if you try to use a simple GET request you'll get a 404.

    In any case, the example was a very nice demo of ajax, you should take it at face value and use it for your own needs.

    Could i just ask, tho, why people prefer jQuery to prototype? 

Page 1 of 1 (8 items)