SmoothFriction.nl

posts - 39, comments - 20, trackbacks - 0

mvc

mvc
ASP.NET MVC: Tricky modelbinding feature (?)

I just ran into a situation which could lead to very annoying latent bugs in your web application. Observe the following code. The view: <form method="post" action="Photo/Save"> <input type="text" name="Photo" /> <input type="text" name="Title" /> </form> The Controller action: public ActionResult Save(PhotoDTO photo) { // do something with the object return RedirectToAction("Index"); } The class: public class PhotoDTO { public string Photo { get; set; } public string Title { get; set; } }   At first sight, there’s nothing wrong with this code. You’d say when the form is posted, the PhotoDTO object gets...

posted @ Thursday, November 26, 2009 12:24 AM | Feedback (0) | Filed Under [ c# asp.net mvc ]

ASP.NET MVC: Access an action only by ajax

In a standard ‘web 2.0’ (god, I hate that buzzword) web application, you’ll often have ajax calls to your code. I prefer using jQuery, but any method will do. A proper ajax request will set a header value in the HTTP GET request, and ASP.NET is a good boy and catches that. By checking if the IsAjaxRequest() method on the request returns true. This feature can be utilized for something I like to do on my ajax-exclusive controller actions: restrict access in such a way that only ajax requests can call the action. Other actions will get a 404,...

posted @ Monday, October 05, 2009 12:00 PM | Feedback (1) | Filed Under [ c# asp.net mvc ]

ASP.NET MVC: Common viewdata

One issue I’ve heard a few times already when explaining ASP.NET MVC to others is how to deal with common viewdata which should be available to every page. Sure, you can simply add it to the viewdata in each and every controller action you call, but that’s a horrible violation of the DRY (Don’t Repeat Yourself) principle. It’s also very error-prone, because you’re very likely to forget to add it some time or another. A more elegant solution to the problem is to have a base controller class which you will use for all your controllers. This is a...

posted @ Friday, October 02, 2009 1:50 AM | Feedback (1) | Filed Under [ c# asp.net mvc ]