1

OK文字列の保護された内部セットを含む複数のモデルにコントローラー/ビューがアタッチされているMVCに問題があります。これらのオブジェクトを作成するときは、文字列を設定できるようにする必要があります。これを達成するためにModelBindingを理解するのに問題があると言った。ModelBinderの非常に基本的なセットアップを添付しましたが、ここからどこに行くべきかわかりません。

/// <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
}
4

1 に答える 1

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"]

ただし、ここに HttpContext があるため、QueryString または必要なものから値を取得できます。詳細を確認する必要があるすべてのプロパティをデバッグして確認してください。

最後に、このバインダーを設定し、この方法で 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)。

于 2012-05-29T22:39:19.410 に答える