【问题标题】:Custom Binding in ASP.NET MVC - Changing the Binding Behavior on a Single FieldASP.NET MVC 中的自定义绑定 - 更改单个字段的绑定行为
【发布时间】:2009-06-15 12:05:50
【问题描述】:

我的数据库中有一个双精度浮点字段,用于存储午夜过后的秒数。我有 c# 中的代码,可以将此数字转换为 HH:MM:SS.ffffff 的形式,其中 f 是几分之一秒。我也有可以将其解析回双精度的代码。

我想做的是修改 ASP.NET MVC 中的 stock binder 对象,使其使用 MY 解析和 ToString() 方法,而不是通常从 double 类型使用的方法,而无需编写完整的自定义活页夹。

我可以在 c# 中编写几个扩展方法,或者使用几个放置得当的属性让 binder 对象知道我的解析和 ToString() 方法吗?

这里有更多的说明。

如果我向视图提供诸如 Linq to SQL 类之类的模型,MVC 可以自动将我的字段连接到底层数据模型,前提是我在视图中使用与我在我的视图中使用的控件相同的名称数据模型。如果我将 MVC 视图发布回控制器方法,我可以通过调用 Controller 类上的 UpdateModel 方法将用户的更改写回数据库。我不必担心将我的特定字段从 FormCollection 连接到我的 Linq to SQL 数据类,因为活页夹已经为我完成了。

我假设,为了让 binder 对普通数字 double 发挥这种魔力,它必须首先解析来自用户的输入(可能通过调用 double.parse),然后将其传递给 Linq to SQL 类以便它可以更新数据库。

对于模型中的一个字段,我需要使用我自己的自定义解析器,而不是双数据类型附带的解析器。我不想承担剩余领域的责任。

【问题讨论】:

  • 您为什么反对编写自定义活页夹?您可以将大部分逻辑封装在一个类中,模型绑定器将非常简单。
  • 我是否必须接管所有其他字段的绑定才能做到这一点?
  • 不,仅用于绑定到该类型的操作参数。我发布了一些代码。祝你好运

标签: asp.net-mvc c#-3.0


【解决方案1】:

这是一个幼稚、快速和肮脏的例子,但如果你决定走模型绑定路线,试试类似的东西:

// This goes in global.asax
ControllerBuilder.Current.SetControllerFactory(new ControllerFactory());
ModelBinders.Binders.Add(typeof(CustomDateType), new CustomDateBinder());

......

public class CustomDateType
{
    public CustomDateType(string value)
    {
    }

    public string GetText()
    {
        // I do fancy stuff
        throw new NotImplementedException();
    }
    public decimal? GetValue()
    {
        // I do fancy stuff
        throw new NotImplementedException();
    }
}

public class CustomDateBinder : IModelBinder
{
    public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        try
        {
            string doubleDateValue = GetAttemptedValue(bindingContext);

            if (doubleDateValue == "")
            {
                return null;
            }

            return new CustomDateType(doubleDateValue);
        }
        catch (Exception ex)
        {
            string message = string.Format("Unable to locate a valid value for query string parameter '{0}'",
                                       bindingContext.ModelName);
            throw new ApplicationException(message, ex);
        }
    }

    private static string GetAttemptedValue(ModelBindingContext bindingContext)
    {
       ValueProviderResult value = bindingContext.ValueProvider[bindingContext.ModelName];
        return value == null ? string.Empty : value.AttemptedValue;
    }
 }

【讨论】:

    【解决方案2】:

    名字一定要.ToString()吗?如果例如.ToCustomString() 是可以接受的,这很容易:

    public static class DoubleExtensions
    {
        public static string ToCustomString(this double d)
        {
            return "my own";
        }
    }
    

    【讨论】:

    • 在什么情况下使用它?文本输出在哪里(以您要更改的方式)?
    • 好的,但是我如何告诉 MVC binder 使用这个方法而不是它自己的方法呢?实际上,我更关心解析用户的输入。我总是可以在视图中提供格式以供显示。
    • 请提供一些示例代码,其中 .ToString() 方法将被调用,您想要更改输出。没有任何示例代码,很难提供帮助......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多