Tag: MVC
All the articles with the tag "MVC".
-
Partial page updates with ASP.NET MVC and jQuery (and action filters)
When building an ASP.NET MVC application, chances are that you are using master pages. After working on the application for a while, it's time to spice up some views with jQuery and partial updates. Let's start with an example application which does not have any Ajax / jQuery. Our company's website shows a list of all employees and provides a link to a details page containing a bio for that employee. In the current situation, this link is referring to a custom action method which is rendered on a separate page. The company website could be made a little sexier... What about fetching the employee details using an Ajax call and rendering the details in the employee list? Yes, that's actually what classic ASP.NET's UpdatePanel does. Let's do that with jQuery instead.
-
Creating a generic Linq to SQL ModelBinder for the ASP.NET MVC framework
You are right! This is indeed my third post on ASP.NET MVC ModelBinders. The first one focussed on creating a ModelBinder from scratch in an older preview release, the second post did something similar trying to do some dirty ViewState-like stuff. Good news! There's more of this dirty stuff coming! How about this action method, using a Person class which is a Linq to SQL entity type: [code:c#] public ActionResult PersonDetails(Person id) { if (id == null) return RedirectToAction("Index"); return View(id); } [/code]
-
CarTrackr - Sample ASP.NET MVC application
Some people may have already noticed the link in my VISUG session blog post, but for those who didn't... I've released my sample application CarTrackr on CodePlex. CarTrackr is a sample application for the ASP.NET MVC framework using the repository pattern and dependency injection using the Unity application block. It was written for various demos in presentations done by Maarten Balliauw. CarTrackr is an online software application designed to help you understand and track your fuel usage and kilometers driven. You will have a record on when you filled up on fuel, how many kilometers you got in a given tank, how much you spent and how much liters of fuel you are using per 100 kilometer.
-
Introduction to ASP.NET MVC for VISUG - Presentation materials
Yesterday evening, I did a presentation on the ASP.NET MVC framework for VISUG (Visual Studio User Group Belgium). I really hope everyone got a good feel on what the ASP.NET MVC framework is all about and what it takes to build an ASP.NET MVC application. Thank you Pieter Gheysens for inviting me for this talk! And thank you audience for being interested for over an hour and a half! A recorded version of this presentation will be available later, for the moment you'll have to do with the presentation materials. The download contains the slides, the Hello World application and the testing demo. The CarTrackr application can be found on CodePlex. Presentation materials: VISUG ASP.NET MVC materials.zip (5.63 mb) CarTrackr sample application: http://www.codeplex.com/CarTrackr/
-
Using the ASP.NET MVC ModelBinder attribute - Second part
Just after the ASP.NET MVC preview 5 was released, I made a quick attempt to using the ModelBinder attribute. In short, a ModelBinder allows you to use complex objects as action method parameters, instead of just basic types like strings and integers. While my aproach was correct, it did not really cover the whole picture. So here it is: the full picture. First of all, what are these model binders all about? By default, an action method would look like this: [code:c#] public ActionResult Edit(int personId) { // ... fetch Person and do stuff } [/code] Now wouldn't it be nice to pass this Person object completely as a parameter, rather than obliging the controller's action method to process an id? Think of this: [code:c#]
-
Forms interaction with ASP.NET MVC (screencast)
Abstract: "This screencast is a short demonstration on how you can handle form interactions using the ASP.NET MVC framework." Download sample code: MvcCommentForm.zip (593.58 kb)
-
ASP.NET MVC preview 5's AntiForgeryToken helper method and attribute
The new ASP.NET MVC preview 5 featured a number of new HtmlHelper methods. One of these methods is the HtmlHelper.AntiForgeryToken. When you place <%=Html.AntiForgeryToken()%> on your view, this will be rendered similar to the following: [code:c#] <input name="__MVC_AntiForgeryToken" type="hidden" value="Ak8uFC1MQcl2DXfJyOM4DDL0zvqc93fTJd+tYxaBN6aIGvwOzL8MA6TDWTj1rRTq" /> [/code] When using this in conjunction with the action filter attribute [ValidateAntiForgeryToken], each round trip to the server will be validated based on this token. [code:c#] [ValidateAntiForgeryToken] public ActionResult Update(int? id, string name, string email) { // ... } [/code]
-
Using the ASP.NET MVC ModelBinder attribute
ASP.NET MVC action methods can be developed using regular method parameters. In earlier versions of the ASP.NET MVC framework, these parameters were all simple types like integers, strings, booleans, … When required, a method parameter can be a complex type like a Contact with Name, Email and Message properties. It is, however, required to add a ModelBinder attribute in this case. Here’s how a controller action method could look like: [code:c#] public ActionResult Contact([ModelBinder(typeof(ContactBinder))]Contact contact) { // Add data to view ViewData["name"] = contact.Name; ViewData["email"] = contact.Email; ViewData["message"] = contact.Message; ViewData["title"] = "Succes!"; // Done! return View(); } [/code]
-
Form validation with ASP.NET MVC preview 5
In earlier ASP.NET MVC previews, form validation was something that should be implemented "by hand". Since the new ASP.NET MVC preview 5, form validation has become more handy. Let me show you how you can add validation in such a ridiculously easy manner. Here's an example controller: [code:c#] using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using System.Web.Mvc.Ajax; namespace ValidationExample.Controllers { [HandleError] public class HomeController : Controller { // ... some other action methods ... [AcceptVerbs("GET")] public ActionResult Contact() { return View(); }
-
Building an ASP.NET MVC sitemap provider with security trimming
Warning!A new version of the source code provided in this post is available here. Use this blog post as reference only. Yes, it has been a while since my last post. A nice vacation to Austria, some work to catch up, ... All excuses, I know, but I'll make it up to you with a huge blog post! If you have been using the ASP.NET MVC framework, you possibly have been searching for something like the classic ASP.NET sitemap. After you've played with it, you even found it useful! But not really flexible and easy to map to routes and controllers. Sounds familiar? Continue reading! Doesn't ring a bell? Well, continue reading, please! Feel free to download the sample code. UPDATE: A version for preview 5 can also be downloaded: MvcSitemapProvider.cs (19.46 kb)