我有一个允许输入的视图,我的sql Server 2008 R2数据库中的字段设置为float,我正在使用Entity Framwork 4.
在实体框架模型中,该字段看起来像私有Nullable< global :: System.Double> _TestNumber;
View使用EditorField来允许这样的输入:
<div class="editor-field"> @Html.EditorFor(model => model.TestNumber) @Html.ValidationMessageFor(model => model.TestNumber) </div>
但是我在验证消息中收到此错误:值’13 .51’对TestNumber无效.我试过逗号而不是句号,同样的事情.
建议?
解决方法
这应该工作:
查看型号:
public class Myviewmodel { public double? TestNumber { get; set; } }
控制器:
public class HomeController : Controller { public ActionResult Index() { return View(new Myviewmodel { TestNumber = 13.51 }); } [HttpPost] public ActionResult Index(Myviewmodel model) { return View(model); } }
视图:
@model AppName.Models.Myviewmodel @{ ViewBag.Title = "Home Page"; } @using (Html.BeginForm()) { @Html.EditorFor(x => x.TestNumber) @Html.ValidationMessageFor(x => x.TestNumber) <input type="submit" value="OK" /> }
您可以签出并且可以解释您正在观察的行为的一件事是客户端文化和服务器端文化之间的不一致.因此,例如,如果您已启用客户端验证但客户端文化使用,作为小数分隔符,则13.51将无法通过客户端验证,如果服务器区域使用.作为小数分隔符,然后13,51将无法通过服务器端验证.所以13.51和13,51都无法验证,但在不同的层上.为了使服务器使用与客户端相同的文化,您可以在web.config中将以下文化设置为auto:
<globalization requestEncoding="utf-8" responseEncoding="utf-8" culture="auto" uiCulture="auto" />
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。