background preloader

MVC

Facebook Twitter

@Html and beyond | Peter van Ooijen. I like the way MVC builds the content of my web-apps. I write plain HTML, and the @Html helper object will build all Html controls needed for in and output of the dynamic data. The nice thing is that the HtmlHelper object is strongly typed, all properties of the view’s model are available as lambda expressions. @Html.LabelFor(m => m.AanmeldingsKlacht) @Html.TextBoxFor(m => m.AanmeldingsKlacht) Nice clean code.

Things are getting harder when you need more than the standards the HtmlHelper class gives you. The way to extend the HtmlHelper is by using extension methods. The methods of the HtmlHelper class should return strings typed as MvcHtmlStrings. As an example of this is Purple, a very simple extension method which does demonstrate the principles and possibilities. public static MvcHtmlString Purple<TModel, TProperty>( this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression) return new MvcHtmlString( htmlHelper.EditorFor(expression))); Note the following points return. Knockout : Home. Elegant Code, Raccoon Blog’s CSS Controller. The following piece of code is responsible for the CSS generation for this blog: public class CssController : Controller { public ActionResult Merge(string[] files) { var builder = new StringBuilder(); foreach (var file in files) { var pathAllowed = Server.MapPath(Url.Content("~/Content/css")); var normalizeFile = Server.MapPath(Url.Content(Path.Combine("~/Content/css", file))); if (normalizeFile.StartsWith(pathAllowed) == false) { return HttpNotFound("Path not allowed"); } if (System.IO.File.Exists(normalizeFile)) { Response.AddFileDependency(normalizeFile); builder.AppendLine(System.IO.File.ReadAllText(normalizeFile)); } } Response.Cache.VaryByParams["files"] = true; Response.Cache.SetLastModifiedFromFileDependencies(); Response.Cache.SetETagFromFileDependencies(); Response.Cache.SetCacheability(HttpCacheability.Public); var css = dotless.Core.Less.Parse(builder.ToString(), new DotlessConfiguration()); return Content(css, "text/css"); } }

Templated Razor Delegates. David Fowler turned me on to a really cool feature of Razor I hadn’t realized made it into 1.0, Templated Razor Delegates. What’s that? I’ll let the code do the speaking. @{ Func<dynamic, object> b = @<strong>@item</strong>; } <span>This sentence is @b("In Bold"). </span> That could come in handy if you have friends who’ll jump on your case for using the bold tag instead of the strong tag because it’s “not semantic”.

Smile with tongue out. Note that the delegate that’s generated is a Func<T, HelperResult>. The example I showed is pretty trivial. Suppose I wrote this really cool HTML helper method for generating any kind of list. public static class RazorExtensions { public static HelperResult List<T>(this IEnumerable<T> items, Func<T, HelperResult> template) { return new HelperResult(writer => { foreach (var item in items) { template(item).WriteTo(writer); } }); }} This List method accepts a templated Razor delegate, so we can call it like so.

Now this List method is very reusable. Introducing Data Annotations Extensions - Scott's Blog. Wednesday, February 23, 2011 8:12 AM srkirkland Validation of user input is integral to building a modern web application, and ASP.NET MVC offers us a way to enforce business rules on both the client and server using Model Validation. The recent release of ASP.NET MVC 3 has improved these offerings on the client side by introducing an unobtrusive validation library built on top of jquery.validation. Out of the box MVC comes with support for Data Annotations (that is, System.ComponentModel.DataAnnotations) and can be extended to support other frameworks. Data Annotations Validation is becoming more popular and is being baked in to many other Microsoft offerings, including Entity Framework, though with MVC it only contains four validators: Range, Required, StringLength and Regular Expression. The Data Annotations Extensions project attempts to augment these validators with additional attributes while maintaining the clean integration Data Annotations provides.

On to the Preview [Email] if (! Advanced ASP.NET MVC 3 Presentation.