background preloader

JavaScript Development

Facebook Twitter

Model Binding To A List. Download the sample project to play with the code as you read this blog post.

Model Binding To A List

Using the DefaultModelBinder in ASP.NET MVC, you can bind submitted form values to arguments of an action method. But what if that argument is a collection? Can you bind a posted form to an ICollection<T>? Sure thing! It’s really easy if you’re posting a bunch of primitive types. Public ActionResult UpdateInts(ICollection<int> ints) { return View(ints);} You can bind to that by simply submitting a bunch of form fields each having the same name.

If you were to take fiddler and look at what data actually gets posted when clicking the submit button, you’d see the following. ints=1&ints=4&ints=2&ints=8 The default model binder sees all these name/value pairs with the same name and converts that to a collection with the key ints, which is then matched up with the ints parameter to your action method. Where it gets trickier is when you want to post a list of complex types. ASP.NET MVC4 Binding to a List of Complex Types. UPDATE: I have an idea how they might be doing it: Expression Trees!

ASP.NET MVC4 Binding to a List of Complex Types

One of my colleagues and I ran into a little trouble with Model Binding in ASP.NET MVC 4 yesterday. We wanted a form to post back an IEnumberable of a complex type, let’s call it IEnumberable. Doing this naively didn’t work. After a bit of googling, and experimentation we found out how to get what we wanted, but the answer was a bit quirky: you must use an array/collection index operator inside your Html.Whatever() lambda, otherwise the Html helper doesn’t know that it should put an array index at the front of the field names in your form. If it doesn’t put this index (in the form of “[0].”, “[1].”), then the MVC model binder can’t work out how to group your fields into instances of a complex type. Our initial view looked something like this: And the action method it was posting back to looked something like: [HttpPost] public ActionResult Index(IEnumerable model) { return View(model); } So, enter the scientific method: Perfection Kills. Extending builtin natives. Evil or not?

← back 1116 words 8 August 2011 Couple of days ago, Nick Morgan asked my opinion on extending native objects.

Extending builtin natives. Evil or not?

The question came up when trying to answer — “why doesn’t underscore.js extend built-ins”? Why doesn't it define all those Array methods — like map, forEach, every — on Array.prototype. Why does it put them under _ "namespace" — _.each, _.map, _.every, etc. Is it because extending built-in natives is evil? I often see this confusion about extending things in Javascript. There’s a big difference between extending native built-in objects and extending host objects. To avoid any confusion, by native, built-in objects I’m talking about objects and methods introduced in ES5 — Array.prototype extensions (forEach, map, reduce, etc.), Object extensions (Object.create, Object.keys, etc.), Function.prototype.bind, String.prototype.trim, JSON.*, etc.

Well, let’s quickly go over problems with host objects extension: Host vs. So what do we have? Downsides§ Are there any downsides? Enumerability§ JavaScript Module Pattern: In-Depth. The module pattern is a common JavaScript coding pattern.

JavaScript Module Pattern: In-Depth

It’s generally well understood, but there are a number of advanced uses that have not gotten a lot of attention. In this article, I’ll review the basics and cover some truly remarkable advanced topics, including one which I think is original. The Basics We’ll start out with a simple overview of the module pattern, which has been well-known since Eric Miraglia (of YUI) first blogged about it three years ago. If you’re already familiar with the module pattern, feel free to skip ahead to “Advanced Patterns”. Anonymous Closures.