asp.net-mvc – 在Asp.Net MVC中使用千位分隔符的十进制值
我有一个自定义模态类,其中包含一个小数成员和一个视图来接受此类的条目.一切运行良好,直到我添加了 javascripts来格式化输入控件内的数字.格式代码格式化输入的数字与千分隔符’,’当焦点模糊. 问题是,我的模态类中的小数值不会与千分隔符绑定/解析.当我用“1,000.00”测试时,ModelState.IsValid返回false,但是对于“100.00”是无效的. 如果您有任何解决方案,可以与我分享吗? 提前致谢. 样本类 public class Employee { public string Name { get; set; } public decimal Salary { get; set; } } 样品控制器 public class EmployeeController : Controller { [AcceptVerbs(HttpVerbs.Get)] public ActionResult New() { return View(); } [AcceptVerbs(HttpVerbs.Post)] public ActionResult New(Employee e) { if (ModelState.IsValid) // <-- It is retruning false for values with ',' { //Subsequence codes if entry is valid. // } return View(e); } } 样品视图 <% using (Html.BeginForm()) { %> Name: <%= Html.TextBox("Name")%><br /> Salary: <%= Html.TextBox("Salary")%><br /> <button type="submit">Save</button> <% } %> 我试过一个解决方法与自定义ModelBinder亚历山大建议.问题解决了.但是,IDataErrorInfo实现的解决方案并不顺利.由于验证,输入0时,工资值变为空值.有什么建议吗 亚历山大建议的自定义模型绑定器的更新代码 型号粘合剂 public class MyModelBinder : DefaultModelBinder { public override object BindModel(ControllerContext controllerContext,ModelBindingContext bindingContext) { if (bindingContext == null) { throw new ArgumentNullException("bindingContext"); } ValueProviderResult valueResult; bindingContext.ValueProvider.TryGetValue(bindingContext.ModelName,out valueResult); if (valueResult != null) { if (bindingContext.ModelType == typeof(decimal)) { decimal decimalAttempt; decimalAttempt = Convert.ToDecimal(valueResult.AttemptedValue); return decimalAttempt; } } return null; } } 员工班 public class Employee : IDataErrorInfo { public string Name { get; set; } public decimal Salary { get; set; } #region IDataErrorInfo Members public string this[string columnName] { get { switch (columnName) { case "Salary": if (Salary <= 0) return "Invalid salary amount."; break; } return string.Empty; } } public string Error{ get { return string.Empty; } } #endregion } 解决方法似乎总是有某种形式的解决方法被找到,以使默认模型绑定器快乐!我想知道您是否可以创建一个仅由模型绑定器使用的“伪”属性? (注意,这绝对不是优雅的,我自己,似乎越来越多地采取类似的技巧,因为他们工作,他们得到工作“完成”…)还要注意,如果你使用一个单独的“ViewModel”(我建议这样做),你可以把这段代码放在那里,让你的域模型很好,干净.public class Employee { private decimal _Salary; public string MvcSalary // yes,a string. Bind your form values to this! { get { return _Salary.ToString(); } set { // (Using some pseudo-code here in this pseudo-property!) if (AppearsToBeValidDecimal(value)) { _Salary = StripCommas(value); } } } public decimal Salary { get { return _Salary; } set { _Salary = value; } } } 在我打了这个电话之后,我现在回头看看,我甚至犹豫要发贴,真是太丑了!但如果你认为这可能是有帮助的,我会让你决定… 祝你好运!-麦克风 (编辑:台州站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- asp.net-mvc-3 – 在MVC3中使用Html.LabelFor的表单标签中的
- asp.net-mvc – ASP.NET MVC:Action中的授权 – 建议的模式
- asp.net-mvc – 使用输出缓存和其他动作过滤器
- asp.net – 错误:数据绑定方法(如Eval(),XPath()和Bind()只
- 如何排序. .NET中的resx(资源文件)
- asp.net后台cs中的JSON格式变量在前台Js中调用方法(前后台示
- asp.net – 如何在gridview中将navigateurl添加到超链接
- asp.net-mvc – 将DropDownListFor绑定到字典
- asp.net-web-api – 在WebApi OData中为OData服务文档基URL
- asp.net-mvc – 如果用户在int字段中放入非数字字符串,则自
- asp.net webservice返回json的方法
- 在ASP.NET中使用querystring的最佳做法?
- 这是一个bug?浮动操作被视为整数
- 从app_data中删除文件夹时如何防止asp.net重新编
- asp.net-mvc – 不要在ASP .NET MVC 4 BundleCon
- asp.net – 确定当前页面是否需要授权?
- WPAD查询从ASP.NET调用webservice
- asp.net – 避免在web.config中提供服务器连接字
- asp.net-mvc – 神秘的ASP.NET MVC Action高延迟
- asp.net-mvc – 在EditorFor for child对象中使用