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

社会主义新天地 提交于 2019-12-03 05:57:17

问题


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)
{
...
}

回答1:


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

    }
}


来源:https://stackoverflow.com/questions/6739012/how-do-i-use-the-bindinclude-attribute-on-complex-nested-objects

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!