asp.net-mvc ASP.NET MVC 可选字段被视为必需字段

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13293207/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 02:23:53  来源:igfitidea点击:

ASP.NET MVC optional field being treated as required

asp.net-mvcasp.net-mvc-3

提问by user1662812

I have this field that for some reason when I click on submit, gets a validation message that the field is required.

我有这个字段,出于某种原因,当我单击提交时,会收到一条验证消息,表明该字段是必需的。

[DisplayName("Total Budget:")]
public double Budget { get; set; }

@Html.EditorFor(model => model.account.Budget)
@Html.ValidationMessageFor(model => model.account.Budget)

public class Account
{
    [DisplayName("Total Budget:")]
    public double Budget { get; set; } //dropdown
}

回答by Jerad Rose

The built-in DefaultModelBinder in MVC will perform required and data type validation on value types like int, DateTime, decimal, etc. This will happen even if you don't explicitly specify validation using someting like [Required].

MVC 中的内置 DefaultModelBinder 将对 int、DateTime、decimal 等值类型执行必需的和数据类型验证。即使您没有使用类似[Required].

In order to make this optional, you will have to define it as nullable:

为了使此可选,您必须将其定义为可为空的:

public double? Budget { get; set; }

回答by Erik Funkenbusch

doubleis a value type. Value types always contain a value, even if you did not set one. That value is the default value for it's type (in this case 0.0). All value types are treated as required by the framework. The only way around this is to create a custom model binder, but that will not prevent the model from containing the default value (because there is no way to say that it wasn't entered).

double是值类型。值类型总是包含一个值,即使您没有设置一个值。该值是其类型的默认值(在本例中为0.0)。所有值类型都按照框架的要求进行处理。解决这个问题的唯一方法是创建一个自定义模型绑定器,但这不会阻止模型包含默认值(因为没有办法说它没有被输入)。

So even if you create a custom binder, when you process your model, you won't be able to tell if someone entered 0or whether that was just the default value.

因此,即使您创建了自定义活页夹,当您处理模型时,您也无法判断是否有人输入0或者这是否只是默认值。

Thus, the only real solution is to change your view model to use a nullable type, such as Nullable<double>(shorthand is double?).

因此,唯一真正的解决方案是更改您的视图模型以使用可空类型,例如Nullable<double>(简写为double?)。

回答by Nahuel García

You have to add the following line in the application_start (global.asax)

您必须在 application_start (global.asax) 中添加以下行

DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes = false;

Source: Unrequired property keeps getting data-val-required attribute

来源:不需要的属性不断获取 data-val-required 属性

回答by Antarr Byrd

You probably change Budget from a doubleto double?

您可能将预算从 a 更改doubledouble?

You probably can try adding this attribute to the controller

您可能可以尝试将此属性添加到控制器

BindExclude([Bind(Exclude="Budget")])as well

BindExclude([Bind(Exclude="Budget")])还有

回答by Mahesan Rv

Use [NotMapped] annotation , this removes the required validation in the flow you also use own display attributes

使用 [NotMapped] annotation ,这将删除流程中所需的验证,您还使用自己的显示属性

回答by Nabeel Mahmood

Use Nullableor ?after attribute name.

使用Nullable? 在属性名称之后。