How to set default value HTML.EditorFor ()

How can I set the default value in my editors so that in case I don't write anything in this field, it will not send a null value.

<div class="editor-label"> @Html.Label("Description") </div> // somthing like <div>@Html.EditorFor(model => model.Description, " Default value ") @Html.ValidationMessageFor(model => model.Description) </div> 

Or if I go to:

  @Html.TextBoxFor(Model.somthing, "Default value") 
+8
asp.net-mvc razor
source share
4 answers

To display the default value in a field, you must assign the "Value" property in htmlAttributes to it, as in this example:

 @Html.EditorFor(model => model.Description, new { htmlAttributes = new { @class = "form-control", @Value = ViewBag.DefaultDescription } }) 

Verify that the value of V in the value is uppercase.

That way, you are simply assigning a default value to the html field, not to the model.

Assigning default values ​​to the model forces you to first create a model object and will set default values ​​for non-nullable fields, such as dates that will cause the page to show annoyance 1/1/1001 00:00:00 values ​​in datetime fields that you could have in the rest of the model.

+19
source share

The easiest way is to initialize the properties in the model constructor:

 public class PersonModel { public PersonModel () { FirstName = "Default first name"; Description = "Default description"; } public int Id { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public string Description { get; set; } } 

And when you want to send it to a view, for example, in PersonController.Create action-method:

 public ActionResult Create() { var model = new PersonModel(); return View(model); } [HttpPost] public ActionResult Create(PersonModel model) { // do something on post-back } 

That's all. Remember that you need to create a new instance of your model and pass it to the view in order to use its default values. Since the view associated with this action method expects an instance of PersonModel , but when you use the create method as follows:

 public ActionResult Create() { return View(); } 

the view got nothing (I mean null ), so your default values ​​do not exist.

But if you want to do this for complex purposes, for example. using the default value as a watermark, or as @JTMon said you don't want end users to see the default values, you will have other solutions. Please let me know your purpose.

+11
source share

Instead of using

  @Html.EditorFor(model => model.UserId) 

Using

 @Html.TextBoxFor(model => model.UserId, new { @Value = "Prabhat" }) 
+1
source share

How to determine that your model has a default value for its somthing property? in this case you do not need to do anything special. If this is not satisfactory (for example, you do not want the user to see the "default" on the screen), you can have a custom mediator for this model, which is inherited from DefaultModelBinder, override only the OnModelUpdated method, it has something like :

 model.somthg = string.IsNullOrEmpty(model.somthing) ? "Default Value" : model.somthing 

Also note that EditorFor ignores the custom html attributes that you send to it, as far as I know.

0
source share

All Articles