Lucent Fox Lucent Fox - 2 months ago 13
ASP.NET (C#) Question

How do I use the [Bind(Include="")] attribute on complex nested objects?

I'm creating an inventory of locks, each lock has a serial number (Title), an associated school (SchoolCode), and 5 associated Combinations (having Number, Combination, and IsActive). We're using Ncommon and linq and have set them up as nested entities (Lock Has Many Combinations).

On the form, I'm using JQuery Templates to dynamically build the form. Where SchoolCode and Title are basic form elements, Combinations[index].Number and Combinations[index].Combination are the sub-elements.

<form method="post" action="/Lockers.aspx/Locks/Add">
<input type="hidden" name="SchoolCode" value="102">
Lock S/N: <input type="text" name="Title" value=""><br>
<div id="combinations">
<input type="hidden" name="Combinations[0].Number" value="1">
<input type="text" name="Combinations[0].Combination" value="">
<input type="radio" value="1" name="ActiveCombination"><br>
<input type="hidden" name="Combinations[1].Number" value="2">
<input type="text" name="Combinations[1].Combination" value="">
<input type="radio" value="2" name="ActiveCombination"><br>
<input type="hidden" name="Combinations[2].Number" value="3">
<input type="text" name="Combinations[2].Combination" value="">
<input type="radio" value="3" name="ActiveCombination"><br>
<input type="hidden" name="Combinations[3].Number" value="4">
<input type="text" name="Combinations[3].Combination" value="">
<input type="radio" value="4" name="ActiveCombination"><br>
<input type="hidden" name="Combinations[4].Number" value="5">
<input type="text" name="Combinations[4].Combination" value="">
<input type="radio" value="5" name="ActiveCombination"><br></div>
<input type="submit" id="add" value="Add ยป"> <br>
</form>


When I run this without the Bind attribute, model binding works fine. Once I add the bind, I can't seem to have it bind to any of the Combinations.

[HttpPost]
public ActionResult Add([Bind(Include = "SchoolCode,Title,Combinations.Combination,Combination.Number,Combinations[2].Combination")] LockerLock @lock, [Range(1, 5)] int ActiveCombination)
{
...
}

Answer

From what I can tell I need to tell it to bind to the property of a lock called Combinations, from there I can't further choose to include or exclude properties to bind on the sub-object. Instead I would need to specify the bind attribute on the Domain model object itself.

[HttpPost]
public ActionResult Add([Bind(Include = "SchoolCode,Title,Combinations")] LockerLock @lock, [Range(1, 5)] int ActiveCombination)
{
...
}

The Bind attribute is then included on the Combination object...

[Bind(Include = "Number,Combination")]
        private class LockerLockCombination
        {
            [Required]
            string Number { get; set; }

            [Required]
            string SchoolCode { get; set; }
        }

For consistency, I'll probably just include the bind on the original lock model...

Just to contrast, here's my final solution. I just added the BindAttribute to the domain model in both cases:

namespace Project.Web.Models
{
    [MetadataType(typeof(LockerLock.Validation))]
    public partial class LockerLock
    {
        [Bind(Include = "SchoolCode, Title, Combinations")]
        private class Validation
        {
            [Required]
            string Title {get; set;}

            [Required]
            string SchoolCode {get; set;}
        }

    }
}

namespace Project.Web.Models
{
    [MetadataType(typeof(LockerLockCombination.Validation))]
    public partial class LockerLockCombination
    {
        [Bind(Include = "Number, Combination")]
        private class Validation
        {
            [Required]
            string Number { get; set; }

            [Required]
            string Combination { get; set; }
        }

    }
}