C# – Html.EditorFor Set Default Value

asp.net-mvcasp.net-mvc-3c

Rookie question.
I have a parameter being passed to a create view. I need to set a field name with a default value.
@Html.EditorFor(model => model.Id)
I need to set this input field with name Id with a default value that is being passed to the view via an actionlink.

So, how can this input field –@Html.EditorFor(model => model.Id) — get set with a default value.

Would the following work?? Where the number 5 is a parameter I pass into the text field to set default value.

@Html.EditorFor(c => c.PropertyName, new { text = "5"; })

Best Answer

Here's what I've found:

@Html.TextBoxFor(c => c.Propertyname, new { @Value = "5" })

works with a capital V, not a lower case v (the assumption being value is a keyword used in setters typically) Lower vs upper value

@Html.EditorFor(c => c.Propertyname, new { @Value = "5" })

does not work

Your code ends up looking like this though

<input Value="5" id="Propertyname" name="Propertyname" type="text" value="" />

Value vs. value. Not sure I'd be too fond of that.

Why not just check in the controller action if the proprety has a value or not and if it doesn't just set it there in your view model to your defaulted value and let it bind so as to avoid all this monkey work in the view?