S'abonner au Flux RSS

samedi 12 juin 2010

Deletion confirmation with ASP.NET MVC

Here is my approach for deleting stuff with ASP.NET MVC 2.

As mentioned here, the default Delete view isn't quite correct. What I wanted is a deletion solution that:

  • is powered with Ajax for a better user experience
    • we will make a HTTP DELETE on /Items/Delete/12 to delete item #12
  • is accessible for non-JS enabled browsers (we need a confirmation form)
    • when confirmed, the deletion occurs with HTTP POST on /Items/Delete/12 to delete item #12

With those specifications, we can write the following (wrong) controller code:

  1. public class ItemsController : Controller
  2. {
  3. [HttpGet]
  4. public ActionResult Delete(int id) {
  5.  
  6. ViewData.Model = new ConfirmFormModel {
  7. Id = id.ToString(),
  8. BackAction = "Index"
  9. };
  10.  
  11. return View();
  12. }
  13.  
  14. [HttpDelete]
  15. [HttpPost]
  16. public ActionResult Delete(ConfirmFormModel model) {
  17. ViewData.Model = model;
  18.  
  19. // business action here
  20.  
  21. // if we use Ajax, backaction will be null and have
  22. // to prevent a secondary GET request
  23. return model.BackAction == null ? null : RedirectToAction(model.BackAction);
  24. }
  25.  
  26. }
  1. /// <summary>
  2. /// Simple model class to store the Id for a confirmation form.
  3. /// </summary>
  4. public class ConfirmFormModel {
  5.  
  6. /// <summary>
  7. /// Item's ID
  8. /// </summary>
  9. public string Id { get; set; }
  10.  
  11. /// <summary>
  12. /// Confirmation message.
  13. /// </summary>
  14. public string Message { get; set; }
  15.  
  16. /// <summary>
  17. /// The controller action to redirect to if the user wants to cancel.
  18. /// </summary>
  19. public string BackAction { get; set; }
  20.  
  21. }

A created a little model class to store the confirmation message and the item ID; this way I can use it all over my project. You can notice the [HttpDelete] and [HttpPost] on the second Delete() method. Obviously this won't work as asked here. You have to create your own attribute.

So I created the related views with (not-included) javascript. Here is the index:

  1. <!-- Index page -->
  2.  
  3. <ul>
  4. <% for (int i = 0; i < 10; i++) { %>
  5. <li><%= Html.ActionLink("Delete #" + i, "Delete", new { id = i }, new { @class = "deleteLink" })%></li>
  6. <% } %>
  7. </ul>

The delete page where I include a generic partial-view:

  1. <!-- Delete page -->
  2.  
  3. <h3>Confirmation</h3>
  4.  
  5. <% Html.RenderPartial("ConfirmForm", Model); %>

Finally, the partial view:

  1. <!-- Delete form in a partial view -->
  2.  
  3. <% Html.BeginForm(); %>
  4. <fieldset>
  5. <legend>Confirmation</legend>
  6.  
  7. <p><%= Html.Encode(Model.Message ?? "Are you sure you want to delete this?")%></p>
  8.  
  9. <p>
  10. <%= Html.HiddenFor(m => m.Id) %>
  11. <%= Html.HiddenFor(m => m.BackAction) %>
  12. <%= Html.ActionLink("Cancel", Model.BackAction ?? "Index") %>
  13. <input type="submit" name="Continue" value="Continue" />
  14. </p>
  15. </fieldset>
  16. <% Html.EndForm(); %>

Now we have a working solution to confirm some action on a website. Sources attached...

mercredi 6 août 2008

Les fourberies liées à l'abandon d'un nom de domaine

Voilà quelques semaines que j'ai abandonné le domaine sandrockmp4.com et divers mails me sont parvenus. Le plus intéressant est celui que j'ai reçu de trvdomaindiscovery.com :

Recently, SANDROCKMP4.COM expired and went into a domain name auction. We acquired it and, since you own the .net version of this domain name, we wanted to provide you with the opportunity to own the preferred .com version.

Our company specializes in recovering preferred expiring domains and either selling them to individuals such as yourself or building out our own web presence on those valuable domains.

Une introduction similaire aux précédents mails déjà reçu. Mais la suite contient une argumentation que j'ai trouvé assez marrante fourbe :

SANDROCKMP4.COM is a pretty darn good domain name and, the truth is, the .COM is a far stronger version of the name than the .NET is.

* .Com is the strongest brand on the internet. When people think of a website, they intuitively think '.com'. Odds are people trying to get to your website are inadvertently going to SANDROCKMP4.COM because they assume that's where they can find you.

* .Com conveys Professionalism that .net & .org cannot match.

Globalement, la société explique qu'un domaine en .com inspire plus confiance, est plus fort, est plus professionnel qu'un domaine en .net ou .org. Il est vrai que pour des sites commerciaux ou professionnels (e-commerces, technologies propriétaires, entreprises), les .com est le plus populaire. En revanche, je préfère prévilégier le .org pour des projets libres, le .net pour un site web lié à un réseau et les extensions nationales (.fr, .co.uk, ...) lorsqu'un site web n'est pas dédié à une audience mondiale.

If you'd like to own SANDROCKMP4.COM, you can buy it now by covering our acquisition costs and a modest profit.

If you have any interest I encourage you to act quickly because this domain name will only be offered for sale for a limited time.

http://trvdomaindiscovery.com/buy.php?domain=SANDROCKMP4.COM&template=first

Finalement, me revendre mon domaine pour 557 USD me semble quelque peu abusif. Ne perdez pas vos domaines !