代码之家  ›  专栏  ›  技术社区  ›  Abdus Sattar Bhuiyan

如何在成功提交具有浮点型实体的表单后重置表单[[副本]

c#
  •  0
  • Abdus Sattar Bhuiyan  · 技术社区  · 7 年前

    我有这样一个视图模型:

    public class CompanyAccountViewModel
    {
        public string CompanyName { get; set; }
        public float Interval { get; set; }
        public List<string> MobileNo { get; set; }
    }
    

    我在成功提交后清除模型,如下所示:

    modelState.Clear();
    viewModel = new CompanyAccountViewModel();
    

    清除所有输入字段,除了 Interval

    1 回复  |  直到 7 年前
        1
  •  2
  •   nalka    7 年前

    如果要将值初始化为零以外的任何值,则必须使用允许“空”值的数据类型。万一 float 这将是 Nullable<float> 也可以写出来 float? :

    public class CompanyAccountViewModel
    {
        public string CompanyName { get; set; }
        public float? Interval { get; set; }
        public List<string> MobileNo { get; set; }
    }