简体   繁体   English

将Html.EditorFor值设置为空,而不是十进制字段中的0.00

[英]Set Html.EditorFor value empty instead of 0.00 in decimal field

My Html.EditorFor value is 0.00 instead of empty for decimal property as follows: 我的Html.EditorFor值为0.00而不是十进制属性为empty ,如下所示:

截图

How can I set it to empty instead of 0.00? 如何将其设置为空而不是0.00?

This is because decimal is non-nullable type and default value for decimal is 0.00 if you don't provide any explicit value. 这是因为decimalnon-nullable类型,并且如果未提供任何显式值,则decimal默认值为0.00 And in your case when you are initializing your model class you are not providing any value to it and that's why its taking default value 0.00. 在您初始化模型类的情况下,您没有为其提供任何值,这就是为什么它采用默认值0.00的原因。

You can remove the 0.00 from your form input field by simply as follows: 您可以通过以下简单方法从表单输入字段中删除0.00

public class YourModelClass
{
    ..........
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:#.#}")]
    public decimal CurrentSoftMin { get; set; }
    ..........
}

In your code change code to this [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:#.#}")] decimal InterestRate { get; set; } 在您的代码中,将代码更改为此[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:#.#}")] decimal InterestRate { get; set; } [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:#.#}")] decimal InterestRate { get; set; }

and in view 并鉴于

@Html.TextBoxFor(model => model.InterestRate)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM