大佬教程收集整理的这篇文章主要介绍了asp.net-mvc – 如何在ASP.NET MVC控制器中设置十进制分隔符?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我已经解决了the issue with the mapping JavaScript in my views,但如果我现在尝试发布一个编辑的晚餐条目与点作为十进制分隔符控制器失败(抛出InvalidoperationException)更新模型(在updateModel()metod)。我觉得我必须在控制器的某个地方设置正确的文化,我在OnActionExecuTing()中尝试过,但是没有帮助。
public class decimalmodelBinder : DefaultModelBinder { public override object BindModel(ControllerContext controllerContext,ModelBindingContext bindingContext) { object result = null; // Don't do this here! // It might do bindingContext.ModelState.AddModelError // and there is no RemoveModelError! // // result = base.bindModel(controllerContext,bindingContext); String modelName = bindingContext.ModelName; String attemptedValue = bindingContext.ValueProvider.GetValue(modelName).AttemptedValue; // Depending on CultureInfo,the numberdecimalSeparator can be "," or "." // Both "." and "," should be accepted,but aren't. String wantedSeperator = numberFormaTinfo.CurrenTinfo.numberdecimalSeparator; String alternateSeperator = (wantedSeperator == "," ? "." : ","); if (attemptedValue.IndexOf(wantedSeperator) == -1 && attemptedValue.IndexOf(alternateSeperator) != -1) { attemptedValue = attemptedValue.replace(alternateSeperator,wantedSeperator); } try { if (bindingContext.ModelMetadata.IsNullableValueType && String.IsNullOrWhiteSpace(attemptedvalue)) { return null; } result = decimal.Parse(attemptedValue,numberStyles.Any); } catch (FormatException E) { bindingContext.ModelState.AddModelError(modelName,E); } return result; } }
然后在Application.Start()中的Global.asax.cs中:
@H_226_8@modelBinders.binders.Add(typeof(decimal),new decimalmodelBinder()); ModelBinders.binders.Add(typeof(decimal?),new decimalmodelBinder());请注意,代码不是我的,我实际上是在Kristof Neirynck的博客here中找到的。我刚刚编辑了几行,并添加了特定数据类型的binder,而不是替换默认的绑定。
以上是大佬教程为你收集整理的asp.net-mvc – 如何在ASP.NET MVC控制器中设置十进制分隔符?全部内容,希望文章能够帮你解决asp.net-mvc – 如何在ASP.NET MVC控制器中设置十进制分隔符?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。