2012-05-24 46 views
1

好的我有一個MVC的問題,我的控制器/視圖連接到包含受保護的字符串內部集合的多個模型。當這些對象被創建時,我需要能夠設置字符串。話雖如此,我有理解ModelBinding來完成這個問題。我已經附加了ModelBinder的一個非常基本的設置,但不知道從哪裏去:MVC中的綁定字符串字段

/// <summary> 
/// Handles binding for the string variables 
/// </summary> 
public class ActionResultModelBinder : DefaultModelBinder, IModelBinder, ITypedModelBinder 
{ 
    #region Properties 

    /// <summary> 
    /// Gets the type that this model binder's associated with 
    /// </summary> 
    /// <value> 
    /// The type that this model binder's associated with. 
    /// </value> 
    public Type AssociatedType 
    { 
     get 
     { 
      return typeof(string); 
     } 
    } 

    #endregion Properties 

    #region Methods 

    /// <summary> 
    /// Binds the model by using the specified controller context and binding context. 
    /// </summary> 
    /// <param name="controllerContext">The context within which the controller operates. The context information includes the controller, HTTP content, request context, and route data.</param> 
    /// <param name="bindingContext">The context within which the model is bound. The context includes information such as the model object, model name, model type, property filter, and value provider.</param> 
    /// <returns> 
    /// The bound object. 
    /// </returns> 
    /// <exception cref="T:System.ArgumentNullException">The <paramref name="bindingContext "/>parameter is null.</exception> 
    public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) 
    { 
     var boundValue = base.BindModel(controllerContext, bindingContext); 
     return bindingContext.ModelType == typeof(string); 
    } 

    /// <summary> 
    /// Sets the specified property by using the specified controller context, binding context, and property value. 
    /// </summary> 
    /// <param name="controllerContext">The context within which the controller operates. The context information includes the controller, HTTP content, request context, and route data.</param> 
    /// <param name="bindingContext">The context within which the model is bound. The context includes information such as the model object, model name, model type, property filter, and value provider.</param> 
    /// <param name="propertyDescriptor">Describes a property to be set. The descriptor provides information such as the component type, property type, and property value. It also provides methods to get or set the property value.</param> 
    /// <param name="value">The value to set for the property.</param> 
    protected override void SetProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, PropertyDescriptor propertyDescriptor, object value) 
    { 
     if (propertyDescriptor.PropertyType == typeof(string)) 
     { 
      var stringVal = value as string; 
     } 

     base.SetProperty(controllerContext, bindingContext, propertyDescriptor, value); 
    } 

    #endregion Methods 
} 

回答

0

好吧讓我解釋一下。

你想所以首先你需要返回該模型在

public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) 

這裏綁定模型是一個叫FacebookGroupViewModel模型綁定定製綁定的例子:

public class FacebookGroupViewModelBinder : IModelBinder 
    { 
     public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) 
     { 
      var model = new FacebookGroupViewModel(); 
      if(controllerContext.HttpContext.Request.Form.AllKeys.Contains("Friends")) 
      { 
       var friends = controllerContext.HttpContext.Request.Form["Friends"].Split(','); 
       foreach (var friend in friends) 
       { 
        model.FacebookFriendIds.Add(friend); 
       } 


} 
      return model; 
     } 
    } 

在這裏,您可以請參閱我從表單中獲取值:

controllerContext.HttpContext.Request.Form["Friends"] 

但您可以從QuerySt獲取值環或任何你想要的,因爲你在這裏有HttpContext。調試並查看所有屬性,以瞭解更多信息。

最後,您需要設置此活頁夾,並通過這種方式將它與global.asax中的模型相關聯。

ModelBinders.Binders.Add(typeof(FacebookGroupViewModel),new FacebookGroupViewModelBinder()); 

在應用程序啓動方法中。

然後只是使用想象一下,我的控制器是這樣的。

[HttpPost] 
public ActionResult PostFriend(FacebookGroupViewModel model) 
{ 
//here your model is binded by your custom model ready to use. 
} 

工作完成,讓我知道你是否有更多的問題。

的更多信息,你並不真的需要

protected override void SetProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, PropertyDescriptor propertyDescriptor, object value) 

允許您使用默認聯的行爲,並覆蓋某些屬性就像你正在做只是字符串,但爲此,你需要使用原bindModel方法(例如base.BindModel)