asp.net-mvc – asp.net MVC 1.0和2.0货币模型绑定
发布时间:2020-12-16 07:08:31 所属栏目:asp.Net 来源:网络整理
导读:我想创建模型绑定功能,以便用户可以输入’,”’.等等,用于绑定到我的ViewModel的double值的货币值. 我能够通过创建自定义模型绑定器在MVC 1.0中执行此操作,但是自从升级到MVC 2.0后,此功能不再有效. 有没有人有任何想法或更好的解决方案来执行此功能?更好的
|
我想创建模型绑定功能,以便用户可以输入’,”’.等等,用于绑定到我的ViewModel的double值的货币值.
我能够通过创建自定义模型绑定器在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;
}
}
解决方法
你可以尝试一下这些行:
// 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();
}
}
更新: 这是对活页夹的改进(参见前面代码中的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) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net mvc中Forms身份验证身份验证流程
- Asp.Net GridView EditIndex竞争条件?
- asp.net-mvc – 如何在ASP.NET MVC部分视图中使用匿名列表作
- asp.net-mvc – 添加一个默认的SelectListItem
- ASP.NET应用程序在3个物理层上
- asp.net-mvc – Asp.Net Mvc 2 Checkbox在模型中总是假的
- ASP.NET MVC中的asp.net-mvc – 文件大小上传限制:web.con
- asp.net – 错误的System.Web.Mvc更新
- ASP.NET自带对象JSON字符串与实体类的转换
- asp.net-mvc-4 – Asp.Net Web Api – ModelBinders
推荐文章
站长推荐
- asp.net-mvc-3 – EF 4.1 Code First多个多对多关
- asp.net-mvc-4 – 为什么MVC4捆绑和缩小使我的文
- asp.net-mvc – 350错误:’blah.blah.blah’类型
- asp.net – 对于单个页面,requestFiltering allo
- asp.net-mvc – MVC4 Bootstrap示例中基于角色的
- asp.net-mvc – MVC3的官方文档
- asp.net – 为什么aspx代码隐藏文件被声明为部分
- asp.net – 是否有商业部署并使用基于.NET CAS(代
- asp.net – 消息处理程序和Http模块?
- .net – 每个堆栈跟踪线末尾的标志是什么?
热点阅读
