John John
John John

Reputation: 1

Default value for my Html.EditorFor()

I have the following code inside my razor _CreateOrEdit view :-

@Html.EditorFor(model =>model.SystemInfo.ProcessorInfoes.FirstOrDefault().SPEED)

But i need to display the value of zero incase the model.Speed is null, baring in mind that i am sing the same code in my Create and Edit view ?

Thanks

Upvotes: 1

Views: 766

Answers (1)

Andrei
Andrei

Reputation: 44550

Make target field as not nullable. It will put default value if value is not set.

Or implement custom Editor template:

http://buildstarted.com/2010/09/10/overriding-displayfor-and-editorfor-to-create-custom-outputs-for-mvc/

http://blogs.msdn.com/b/nunos/archive/2010/02/08/quick-tips-about-asp-net-mvc-editor-templates.aspx

Upvotes: 1

Related Questions