如何修复“值无效”错误cshtml?

时间:2019-08-29 09:00:10

标签: c# asp.net razor

我有一个运行sql server数据库的dotnet mvc应用程序。在仅一种输入形式中,我无法提交输入,浏览器给出了一个错误,“值无效”

在模型中:

[Range(0,Int16.MaxValue)]
public Int16? price{ get; set; }

在模型视图中:

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

所有其他表格都运行良好,我在做什么错了?

2 个答案:

答案 0 :(得分:0)

经过数小时的尝试,我已经找到了问题。 在我的模型中,该字段的名称为“价格”。在我的控制器中;

SELECT FOR UPDATE

问题是我在控制器中给参数中的Price表实例指定了名称“ price”。我将其更改为其他内容,问题就消失了。

答案 1 :(得分:0)

您有一个可为空的Int16,这可能是有问题的,您可能为price属性提供了空间或为null。

输入null检查值是否为null,默认情况下应为零。 像这样:

@Html.EditorFor(model => model.price ?? 0)