无法将 属性 保存为小数

Cannot save property as decimal

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

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

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

<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>

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

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

看看这个urlhttp://msdn.microsoft.com/en-us/library/gg674880(VS.98).aspx and https://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); }

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

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

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

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

<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;