views:

33

answers:

2

I have the following code:

<div class="editor-field">
<%: Html.TextBoxFor(model => model.MyId) %>
<%: Html.ValidationMessageFor(model => model.MyId) %>
<
/div>

The 'MyId' property of the model is of type integer.

When the form is in 'Create' mode, MyId value is 0. How can I, prevent 0 displaying and rather render the textbox with an empty string / blank / no value?

I have tried various forms of String.Format without success.

+3  A: 

You could use a nullable integer:

public int? MyId { get; set; }
Darin Dimitrov
Cheers Darin. Works a treat.
Grant Sutcliffe
Wouldn't have suggested it otherwise.
Darin Dimitrov
+1  A: 

Maybe you could use the TextBox() method instead, which allows you to specify the value to be shown:

<%: Html.TextBox("MyId", model.MyId == 0 ? "" : model.MyId.ToString()) %>
M4N
Brilliant. Thanks. That was the answer.
Grant Sutcliffe