Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)
{
...
}
like image 787
Lucent Fox Avatar asked Jul 18 '11 20:07

Lucent Fox


People also ask

What is the use of bind attribute in MVC?

Bind Attribute The [Bind] attribute will let you specify the exact properties of a model should include or exclude in binding. In the following example, the Edit() action method will only bind StudentId and StudentName properties of the Student model class.

What is bind property in C#?

Model binding allows you map request parameters to actions. This means action methods will have one or more parameters and those parameters will receive their values from the model binding framework.

What is bind in ASP NET MVC?

What Is Model Binding? ASP.NET MVC model binding allows mapping HTTP request data with a model. It is the procedure of creating . NET objects using the data sent by the browser in an HTTP request. Model binding is a well-designed bridge between the HTTP request and the C# action methods.


1 Answers

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; }
        }

    }
}
like image 112
Lucent Fox Avatar answered Sep 21 '22 18:09

Lucent Fox