C# 用逗号MVC 5格式化十进制值
我有个问题! 我使用的是Mvc5,我有这样一个属性C# 用逗号MVC 5格式化十进制值,c#,asp.net,asp.net-mvc,string-formatting,C#,Asp.net,Asp.net Mvc,String Formatting,我有个问题! 我使用的是Mvc5,我有这样一个属性 [DisplayFormat(DataFormatString = "{0:n2}", ApplyFormatInEditMode = true)] public decimal Total { get; set; } 和剃刀: @Html.EditorFor(modelItem => Model.Total, new { htmlAttributes = new { @class = "form-control input-sm" }
[DisplayFormat(DataFormatString = "{0:n2}", ApplyFormatInEditMode = true)]
public decimal Total { get; set; }
和剃刀:
@Html.EditorFor(modelItem => Model.Total, new { htmlAttributes = new { @class = "form-control input-sm" } })
在这一点上没有错误。但是如果我发送1.300,40,我总是得到0
但如果我发送1300,40,我会得到正确的值。
我怎样才能解决它?我希望得到正确的值,如果我发送1300,50或1.300,40,您必须添加自己的
ModelBinder
:
public class DecimalModelBinder : IModelBinder
{
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
var valueResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
var modelState = new ModelState { Value = valueResult };
decimal actualValue = 0;
try
{
actualValue = Convert.ToDecimal(valueResult.AttemptedValue,
CultureInfo.CurrentCulture);
}
catch (FormatException e)
{
modelState.Errors.Add(e);
}
bindingContext.ModelState.Add(bindingContext.ModelName, modelState);
return actualValue;
}
}
并在应用程序中注册它。\u开始:
ModelBinders.Binders.Add(typeof(decimal), new DecimalModelBinder());
参考资料:如果我们想为所有c值类型使用通用ModelBinder,那么
using System;
using System.Globalization;
using System.Web.Mvc;
public class ValueTypeModelBinder<T> : IModelBinder
{
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
var valueResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
var modelState = new ModelState { Value = valueResult };
var result = default(T);
try
{
var srcValue = valueResult.AttemptedValue;
var targetType = typeof(T);
//Hp --> Logic: Check whether target type is nullable (or) not?
//If Yes, Take underlying type for value conversion.
if (targetType.IsGenericType &&
targetType.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
{
targetType = Nullable.GetUnderlyingType(targetType);
}
if (targetType.IsValueType && (!string.IsNullOrWhiteSpace(srcValue)))
{
result = (T)Convert.ChangeType(srcValue, targetType, CultureInfo.CurrentUICulture);
}
}
catch (Exception ex)
{
modelState.Errors.Add(ex);
}
bindingContext.ModelState.Add(bindingContext.ModelName, modelState);
return result;
}
}
使用系统;
利用制度全球化;
使用System.Web.Mvc;
公共类ValueTypeModelBinder:IModelBinder
{
公共对象绑定模型(ControllerContext ControllerContext,ModelBindingContext bindingContext)
{
var valueResult=bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
var modelState=newmodelstate{Value=valueResult};
var结果=默认值(T);
尝试
{
var srcValue=valueResult.AttemptedValue;
var targetType=typeof(T);
//Hp-->逻辑:检查目标类型是否可为空(或不可为空)?
//如果是,则采用基础类型进行值转换。
如果(targetType.IsGenericType&&
targetType.GetGenericTypeDefinition().Equals(typeof(Nullable)))
{
targetType=Nullable.GetUnderlinegType(targetType);
}
if(targetType.IsValueType&(!string.IsNullOrWhiteSpace(srcValue)))
{
结果=(T)Convert.ChangeType(srcValue、targetType、CultureInfo.CurrentUICulture);
}
}
捕获(例外情况除外)
{
modelState.Errors.Add(ex);
}
bindingContext.ModelState.Add(bindingContext.ModelName,ModelState);
返回结果;
}
}
添加上述类后,在Global.asax.cs文件中的“Application\u Start”方法下配置ModelBinder
using System.Web;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
public class MvcApplication : HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
ModelBinders.Binders.Add(typeof(decimal?), new ValueTypeModelBinder<decimal?>());
ModelBinders.Binders.Add(typeof(decimal), new ValueTypeModelBinder<decimal>());
ModelBinders.Binders.Add(typeof(int), new ValueTypeModelBinder<int>());
ModelBinders.Binders.Add(typeof(double?), new ValueTypeModelBinder<double?>());
}
}
使用System.Web;
使用System.Web.Mvc;
使用System.Web.Optimization;
使用System.Web.Routing;
公共类MVC应用程序:HttpApplication
{
受保护的无效应用程序\u Start()
{
RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
ModelBinders.Binders.Add(typeof(decimal?),新值typemodelbinder();
添加(typeof(decimal),新值typemodelbinder());
添加(typeof(int),新值typemodelbinder());
添加(typeof(double?),新值typemodelbinder();
}
}
你能确认
只是格式化为千位分隔,而,
是十进制分隔吗?是的,你说得对。我不想像这个函数那样使用when-before-onsubmit$(this.val($(this.val().replace(“,”););进一步阅读:和。基本上,模型绑定系统不支持格式化成千个现成的分隔符。您可以应用自己的类型描述符和/或模型绑定器来处理这种情况。非常感谢您@查德哇!我一直想要这样的东西!祝贺你,我的朋友。