加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – 根据需要处理ASP.NET MVC可选字段

发布时间:2020-12-15 18:38:35 所属栏目:asp.Net 来源:网络整理
导读:我有这个字段,由于某种原因,当我点击提交,获得一个验证消息,该字段是必需的。 [DisplayName("Total Budget:")]public double Budget { get; set; }@Html.EditorFor(model = model.account.Budget)@Html.ValidationMessageFor(model = model.account.Budg
我有这个字段,由于某种原因,当我点击提交,获得一个验证消息,该字段是必需的。
[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

    }

解决方法

MVC中的内置DefaultModelBinder将对类型为int,DateTime,decimal等的类型执行必需的和数据类型的验证。即使没有使用[Required]来明确指定验证,也会发生这种情况。

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

public double? Budget { get; set; }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读