2012-07-25 12 views
7

पर बाध्यकारी है हमें नैन्सी के डिफ़ॉल्ट मॉडल बाइंडर के साथ कोई समस्या है। नीचे दिए गए ...नैन्सी मॉडल बाल वर्ग

public class Foo 
{ 
    public Foo() 
    { 
    } 

    public string Name { get; set; } 

    public Bar Bar { get; set; } 
} 

public class Bar 
{ 
    public string Name { get; set; } 
} 
जैसे तत्वों के साथ

...

<input type="text" value="Name" /> 
<input type="text" value="Bar.Name" /> 
डिफ़ॉल्ट मॉडल इतना की तरह इस्तेमाल किया बांधने की मशीन के साथ

..

var foo = this.Bind<Foo>();

यह सही ढंग से फू बांधता है। नाम लेकिन Foo.Bar.Name

बाध्य करने में विफल रहता है Ena के लिए कोई तरीका है डिफ़ॉल्ट बाइंडर के साथ बाध्यकारी इस प्रकार का खून बह रहा है या क्या हमें अपना खुद का रोल करने की आवश्यकता है? यदि ऐसा कोई अच्छा उदाहरण है?

+0

जब आप क्लाइंट पर जावास्क्रिप्ट ऑब्जेक्ट मॉडल भरते हैं तो आपको शायद बेहतर भाग्य मिलेगा और फिर उसे जेसन या एक्सएमएल में सबमिट करें। मुझे निश्चित रूप से पता है कि बच्चे की वस्तुएं तब बाध्य हो जाएंगी। – albertjan

उत्तर

11

ऐसा क्यों न करें। मुझे पूरा यकीन है कि रिकर्सन को अनुकूलित किया जा सकता है, और यह कुछ ऐसा होगा जहां यह काम नहीं करता है, और इसे इमेडेलबिन्डर की तुलना में कहीं अधिक स्पष्ट किया जा सकता है, लेकिन यह मूल रूप से वही करता है जो आप चाहते हैं।

/// <summary> 
/// Sample model binder that manually binds customer models 
/// </summary> 
public class CustomModelBinder : IModelBinder 
{ 
    /// <summary> 
    /// Bind to the given model type 
    /// </summary> 
    /// <param name="context">Current context</param> 
    /// <param name="modelType">Model type to bind to</param> 
    /// <param name="blackList">Blacklisted property names</param> 
    /// <returns>Bound model</returns> 
    public object Bind(NancyContext context, Type modelType, params string[] blackList) 
    { 
     var parentObject = Activator.CreateInstance(modelType); 

     foreach (var key in context.Request.Form) 
     { 
      var value = context.Request.Form[key]; 
      this.SetObjectValue(parentObject, modelType, key, value); 
     } 

     return parentObject; 
    } 

    /// <summary> 
    /// Sets the object value. 
    /// </summary> 
    /// <param name="instance">The instance.</param> 
    /// <param name="type">The type.</param> 
    /// <param name="key">Name of the property.</param> 
    /// <param name="propertyValue">The property value.</param> 
    private void SetObjectValue(object instance, Type type, string key, string propertyValue) 
    { 
     if (key.Contains(".")) 
     { 
      this.SetObjectValueDeep(instance, type, key, propertyValue); 
     } 

     PropertyInfo propertyInfo = type.GetProperty(key); 
     if (propertyInfo == null) 
     { 
      return; 
     } 

     propertyInfo.SetValue(instance, Convert.ChangeType(propertyValue, propertyInfo.PropertyType), null); 
    } 

    /// <summary> 
    /// Sets the object value derp. 
    /// </summary> 
    /// <param name="instance">The instance.</param> 
    /// <param name="type">The type.</param> 
    /// <param name="key">The key.</param> 
    /// <param name="propertyValue">The property value.</param> 
    private void SetObjectValueDeep(object instance, Type type, string key, string propertyValue) 
    { 
     var propList = key.Split('.').ToList(); 

     PropertyInfo propertyInfo = type.GetProperty(propList.First()); 
     if (propertyInfo == null) 
     { 
      return; 
     } 

     var childObject = propertyInfo.GetValue(instance, null); 

     if (childObject == null) 
     { 
      childObject = Activator.CreateInstance(propertyInfo.PropertyType); 
      propertyInfo.SetValue(instance, childObject, null); 
     } 

     propList.RemoveAt(0); 

     var newKey = propList.Aggregate(string.Empty, (current, prop) => current + (prop + ".")).TrimEnd('.'); 

     if (newKey.Contains(".")) 
     { 
      this.SetObjectValueDeep(childObject, childObject.GetType(), newKey, propertyValue); 
     } 
     else 
     { 
      this.SetObjectValue(childObject, childObject.GetType(), newKey, propertyValue); 
     } 
    } 

    /// <summary> 
    /// Determines whether this instance can bind the specified model type. 
    /// </summary> 
    /// <param name="modelType">Type of the model.</param> 
    /// <returns> 
    /// <c>true</c> if this instance can bind the specified model type; otherwise, <c>false</c>. 
    /// </returns> 
    public bool CanBind(Type modelType) 
    { 
     return true; 
    } 
} 
+0

यह बहुत अच्छा है, धन्यवाद। सूचियों को संभालने और गिटहब पर पोस्ट जोड़ने का विचार हो सकता है। –

संबंधित मुद्दे