简体   繁体   English

MVC 3 Razor,不显示默认值

[英]MVC 3 Razor, don't show default value

I'm using Html.TextBoxFor(model => model.field) which works fine, however it defaults to 0 for numbers, and to N/A for strings if they are not set, how can i possible change that such it doesn't show anything if the value is not set?我正在使用 Html.TextBoxFor(model => model.field) 工作正常,但是它默认为数字 0,如果未设置字符串,则默认为 N/A,我怎么能改变它呢?如果未设置值,则不显示任何内容?

You have to make the values in your model nullable, like so:您必须使 model 中的值可以为空,如下所示:

public class Model
{
    public int? Field { get; set; }
}

That way an empty value will map to the null value and vice versa.这样一个空值将 map 到 null 值,反之亦然。

Change your model so it has nullable properties:更改您的 model 使其具有可为空的属性:

class YourModel {
    [Required]
    public int? Integer { get; set; }
}

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

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