ASP.NET MVC @html.editorfor 像必需的那样工作,我希望它不是必需的
ASP.NET MVC @html.editorfor acts like required and i want it to be not required
<div class="box-cell box1">
<div class="wrap-input100 validate-input">
@Html.LabelFor(model => model.Bags_Per_Valve, htmlAttributes: new { @class = "label-input101" })
@Html.EditorFor(model => model.Bags_Per_Valve, new { htmlAttributes = new { @class = "form-control", @type = "number", @min = "1", @name = "Bags" } })
</div>
</div>
因此,如果您的字段 Bags_Per_valve 不可为空,则默认情况下,如果您未指定任何值,它将设置为零,并且由于 min 属性,它将失败,因为最小值应为 1
让我给你演示一下。我有一个指定年龄的人物模型,年龄不为空
public class Person
{
[Key]
public int id { get; set; }
public int Age { get; set; }
}
现在看来我用了你同样的逻辑
@Html.EditorFor(model => model.Age, new { htmlAttributes = new { @class = "form-control",@type = "number", @min = "1", } })
现在在 UI 它会告诉我这个字段是必需的
但现在,如果我将我的字段更改为接受空值,它就不会失败。所以像这样改变了我的 Person 模型
public class Person
{
[Key]
public int id { get; set; }
public int? Age { get; set; }
}
现在工作正常,因为 Age
的默认值为 null
<div class="box-cell box1">
<div class="wrap-input100 validate-input">
@Html.LabelFor(model => model.Bags_Per_Valve, htmlAttributes: new { @class = "label-input101" })
@Html.EditorFor(model => model.Bags_Per_Valve, new { htmlAttributes = new { @class = "form-control", @type = "number", @min = "1", @name = "Bags" } })
</div>
</div>
因此,如果您的字段 Bags_Per_valve 不可为空,则默认情况下,如果您未指定任何值,它将设置为零,并且由于 min 属性,它将失败,因为最小值应为 1
让我给你演示一下。我有一个指定年龄的人物模型,年龄不为空
public class Person
{
[Key]
public int id { get; set; }
public int Age { get; set; }
}
现在看来我用了你同样的逻辑
@Html.EditorFor(model => model.Age, new { htmlAttributes = new { @class = "form-control",@type = "number", @min = "1", } })
现在在 UI 它会告诉我这个字段是必需的
但现在,如果我将我的字段更改为接受空值,它就不会失败。所以像这样改变了我的 Person 模型
public class Person
{
[Key]
public int id { get; set; }
public int? Age { get; set; }
}
现在工作正常,因为 Age
的默认值为 null