首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将参数传递给action参数上的模型绑定器

将参数传递给action参数上的模型绑定器
EN

Stack Overflow用户
提问于 2009-01-08 08:35:19
回答 3查看 2.5K关注 0票数 2

我希望使用直接在action方法参数上创建的模型绑定器。例如:

代码语言:javascript
复制
public ActionResult MyAction([ModelBinder(typeof(MyBinder))] string param1)

但是,我需要将一个字符串传递给绑定器本身,所以我想知道您是否可以这样做:

代码语言:javascript
复制
public ActionResult MyAction([MyBinder("mystring")] string param1)

有可能吗?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2009-01-08 13:39:02

不能,您不能通过属性声明传递参数。如果您查看ModelBinderAttribute的源代码,您将看到它的构造函数只接受一个类型参数,它没有其他属性,而且它是一个密封类。所以这条路是条死胡同。

据我所知,获取信息到ModelBinder中的唯一方法是FormCollection本身。

但是,您可以创建一个父绑定器类型,并为要使用的每个参数值创建子类型。它很混乱,但在您给出的示例中它可以工作。

票数 2
EN

Stack Overflow用户

发布于 2018-06-26 23:28:20

是的,这是可能的。您应该创建一个由System.Web.Mvc.CustomModelBinder驱动的类,并覆盖GetBinder方法。例如,这里的实现除了Type对象之外,还有一个用于模型绑定器构造函数参数的对象数组:

代码语言:javascript
复制
public sealed class MyModelBinderAttribute : CustomModelBinderAttribute
{
    /// <summary>
    /// Gets or sets the type of the binder.
    /// </summary>
    /// <returns>The type of the binder.</returns>
    public Type BinderType { get; }
    /// <summary>
    /// Gets or sets the parameters for the model binder constructor.
    /// </summary>
    public object[] BinderParameters { get; }

    /// <summary>
    /// Initializes a new instance of the <see cref="T:System.Web.Mvc.ModelBinderAttribute" /> class.
    /// </summary>
    /// <param name="binderType">The type of the binder.</param>
    /// <param name="binderParameters">The parameters for the model binder constructor.</param>
    /// <exception cref="T:System.ArgumentNullException">The <paramref name="binderType" /> parameter is null.</exception>
    public MyModelBinderAttribute(Type binderType, params object[] binderParameters)
    {
        if (null == binderType)
        {
            throw new ArgumentNullException(nameof(binderType));
        }

        if (!typeof(IModelBinder).IsAssignableFrom(binderType))
        {
            throw new ArgumentException(string.Format(CultureInfo.CurrentCulture,
                "An error occurred when trying to create the IModelBinder '{0}'. Make sure that the binder has a public parameterless constructor.",
                binderType.FullName), nameof(binderType));
        }
        this.BinderType = binderType;
        this.BinderParameters = binderParameters ?? throw new ArgumentNullException(nameof(binderParameters));
    }

    /// <summary>Retrieves an instance of the model binder.</summary>
    /// <returns>A reference to an object that implements the <see cref="T:System.Web.Mvc.IModelBinder" /> interface.</returns>
    /// <exception cref="T:System.InvalidOperationException">An error occurred while an instance of the model binder was being created.</exception>
    public override IModelBinder GetBinder()
    {
        IModelBinder modelBinder;
        try
        {
            modelBinder = (IModelBinder)Activator.CreateInstance(this.BinderType, this.BinderParameters);
        }
        catch (Exception ex)
        {
            throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture,
                "An error occurred when trying to create the IModelBinder '{0}'. Make sure that the binder has a public parameterless constructor.",
                this.BinderType.FullName), ex);
        }
        return modelBinder;
    }
}
票数 0
EN

Stack Overflow用户

发布于 2012-03-07 18:03:38

这只是一个想法,我正在考虑需要一个类似的需求,我想要传递一个城堡式的服务。

我想知道您是否可以使用传递到绑定器中的ControllerContext,并通过属性公开服务/属性或其他任何东西?

只是一个想法,我现在要尝试一下,我会给你反馈的。

富足

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/423591

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档