【问题标题】:Cannot save property as decimal无法将属性保存为小数
【发布时间】:2017-09-18 19:32:37
【问题描述】:

在我的模型中,我使用属性。

[Display(Name = "Pensja")]
public decimal? Salary { get; set; }

在编辑 Razor 视图中,我想将值从 4000,00 更改为 4000,50。 在 View 中看起来像这样。

<div class="form-group">
    @Html.LabelFor(model => model.Salary, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.EditorFor(model => model.Salary, new { htmlAttributes = new { @class = "form-control" } })
        @Html.ValidationMessageFor(model => model.Salary, "", new { @class = "text-danger" })
    </div>
</div>

我收到消息说薪水必须是一个数字。我做错了什么?

【问题讨论】:

  • 你使用 Html.BeginForm 吗?你如何在控制器上发送数据?请详细更新问题。
  • 我使用 Html.BeginForm
  • 此消息是客户端验证错误消息?

标签: c# entity-framework razor html-helper


【解决方案1】:

我假设您的错误消息在客户端 (jscipt) 并且认为您的语言是波兰语,因此您可能需要配置一些与您的语言环境相关的东西。

看看这个网址http://msdn.microsoft.com/en-us/library/gg674880(VS.98).aspxhttps://weblogs.asp.net/scottgu/jquery-globalization-plugin-from-microsoft

或禁用您的客户端验证

  @{ Html.EnableClientValidation(false); }
    @Html.EditorFor(model => model.Salary, new { htmlAttributes = new { @class = "form-control" } })
    @{ Html.EnableClientValidation(true); }

【讨论】:

  • 是的,我的语言是波兰语。
  • 另一个选项是禁用客户端验证或添加自定义的验证
  • 我应该删除 ValidationMessageFor 和其他内容吗?
  • 我编辑了答案以显示如何禁用客户端验证
【解决方案2】:

我检查了你的问题,这条消息是 Client validation 消息,它可能是真的。

您必须使用正确 valdiation 脚本,因为货币分隔符与国家/地区不同

请查看这篇文章validation-polish 看看这个https://jqueryvalidation.org/

【讨论】:

  • 我在哪里可以找到它?
【解决方案3】:

我还找到了另一种解决方案。 在视图中。

<div class="form-group">
                    @Html.LabelFor(model => model.Salary, htmlAttributes: new { @class = "control-label col-md-2" })
                    <div class="col-md-10">
                        <input type="text" name="UserSalary" id="UserSalary" value="@Model.Salary"/>
                    </div>
                </div>

在控制器中。

decimal salary;
                Decimal.TryParse(Request.Form["UserSalary"].ToString(), NumberStyles.Any, new CultureInfo("pl-PL"), out salary);
                user.Salary = salary;

【讨论】:

    猜你喜欢
    • 2017-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-06
    相关资源
    最近更新 更多