asp.net MVC 1.0 и 2.0 валютная модель привязки - PullRequest
5 голосов
/ 16 марта 2010

Я хотел бы создать функциональность привязки модели, чтобы пользователь мог вводить ',' '.' и т. д. для валютных значений, которые связаны с двойным значением моей ViewModel.

Мне удалось сделать это в MVC 1.0, создав пользовательский механизм связывания моделей, однако после обновления до MVC 2.0 эта функция больше не работает.

У кого-нибудь есть идеи или лучшие решения для выполнения этой функции? Лучшим решением было бы использовать некоторую аннотацию данных или пользовательский атрибут.

public class MyViewModel
{
    public double MyCurrencyValue { get; set; }
}

Предпочтительным решением было бы что-то вроде этого ...

public class MyViewModel
{
    [CurrencyAttribute]
    public double MyCurrencyValue { get; set; }
}

Ниже мое решение для привязки модели в MVC 1.0.

public class MyCustomModelBinder : DefaultModelBinder
{
    public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        object result = null;

        ValueProviderResult valueResult;
        bindingContext.ValueProvider.TryGetValue(bindingContext.ModelName, out valueResult);
        bindingContext.ModelState.SetModelValue(bindingContext.ModelName, valueResult);

        if (bindingContext.ModelType == typeof(double))
        {
            string modelName = bindingContext.ModelName;
            string attemptedValue = bindingContext.ValueProvider[modelName].AttemptedValue;

            string wantedSeperator = NumberFormatInfo.CurrentInfo.NumberDecimalSeparator;
            string alternateSeperator = (wantedSeperator == "," ? "." : ",");

            try
            {
                result = double.Parse(attemptedValue, NumberStyles.Any);
            }
            catch (FormatException e)
            {
                bindingContext.ModelState.AddModelError(modelName, e);
            }
        }
        else
        {
            result = base.BindModel(controllerContext, bindingContext);
        }

        return result;
    }
}

1 Ответ

7 голосов
/ 16 марта 2010

Вы можете попробовать что-то из строк:

// Just a marker attribute
public class CurrencyAttribute : Attribute
{
}

public class MyViewModel
{
    [Currency]
    public double MyCurrencyValue { get; set; }
}


public class CurrencyBinder : DefaultModelBinder
{
    protected override object GetPropertyValue(
        ControllerContext controllerContext, 
        ModelBindingContext bindingContext, 
        PropertyDescriptor propertyDescriptor, 
        IModelBinder propertyBinder)
    {
        var currencyAttribute = propertyDescriptor.Attributes[typeof(CurrencyAttribute)];
        // Check if the property has the marker attribute
        if (currencyAttribute != null)
        {
            // TODO: improve this to handle prefixes:
            var attemptedValue = bindingContext.ValueProvider
                .GetValue(propertyDescriptor.Name).AttemptedValue;
            return SomeMagicMethodThatParsesTheAttemptedValue(attemtedValue);
        }
        return base.GetPropertyValue(
            controllerContext, 
            bindingContext, propertyDescriptor, 
            propertyBinder
        );
    }
}

public class HomeController: Controller
{
    [HttpPost]
    public ActionResult Index([ModelBinder(typeof(CurrencyBinder))] MyViewModel model)
    {
        return View();
    }
}

UPDATE:

Вот улучшение связующего (см. Раздел TODO в предыдущем коде):

if (!string.IsNullOrEmpty(bindingContext.ModelName))
{
    var attemptedValue = bindingContext.ValueProvider
        .GetValue(bindingContext.ModelName).AttemptedValue;
    return SomeMagicMethodThatParsesTheAttemptedValue(attemtedValue);
}

Для обработки коллекций вам необходимо зарегистрировать подшивку в Application_Start, поскольку вы больше не сможете украшать список с помощью ModelBinderAttribute:

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();
    RegisterRoutes(RouteTable.Routes);
    ModelBinders.Binders.Add(typeof(MyViewModel), new CurrencyBinder());
}

И тогда ваше действие может выглядеть так:

[HttpPost]
public ActionResult Index(IList<MyViewModel> model)
{
    return View();
}

Подводя итог важной части:

bindingContext.ValueProvider.GetValue(bindingContext.ModelName)

Дальнейшим шагом по улучшению этого связывателя будет обработка валидации (AddModelError / SetModelValue)

...