Tôi muốn tạo chức năng ràng buộc mô hình để người dùng có thể nhập ',' '.' vv cho các giá trị tiền tệ liên kết với một giá trị gấp đôi của ViewModel của tôi.asp.net MVC 1.0 và 2.0 mô hình tiền tệ ràng buộc
Tôi có thể thực hiện việc này trong MVC 1.0 bằng cách tạo một trình kết nối mô hình tùy chỉnh, tuy nhiên kể từ khi nâng cấp lên MVC 2.0, chức năng này không còn hoạt động nữa.
Có ai có bất kỳ ý tưởng hay giải pháp nào tốt hơn để thực hiện chức năng này không? Một giải pháp tốt hơn là sử dụng một số chú thích dữ liệu hoặc thuộc tính tùy chỉnh.
public class MyViewModel
{
public double MyCurrencyValue { get; set; }
}
Một giải pháp ưu tiên sẽ là một cái gì đó như thế này ...
public class MyViewModel
{
[CurrencyAttribute]
public double MyCurrencyValue { get; set; }
}
Dưới đây là giải pháp của tôi cho ràng buộc trong MVC 1.0 model.
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;
}
}
Nếu bạn đang xử lý danh sách MyViewModel, điều đó có thay đổi ModelBinder cho hành động không? Chỉ mục hành động công khai ([ModelBinder (typeof (CurrencyBinder))] IList mô hình) –
David
Vui lòng xem cập nhật của tôi. –