Torben Pi Jensen
Torben Pi Jensen

Reputation: 870

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?

Upvotes: 6

Views: 2844

Answers (2)

m0sa
m0sa

Reputation: 10940

Change your model so it has nullable properties:

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

Upvotes: 5

fretje
fretje

Reputation: 8372

You have to make the values in your model nullable, like so:

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

That way an empty value will map to the null value and vice versa.

Upvotes: 8

Related Questions