how to set default value HTML.EditorFor()

2019-03-15 23:33发布

问题:

How can I set a default value in my editorboxes so that in case I don't write anything in the box it doesn't 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 change to:

 @Html.TextBoxFor(Model.somthing, "Default value")

回答1:

To display a default value in a field you must assign 'Value' property in htmlAttributes for it like in this example:

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

Make sure that the V in Value is uppercase.

This way you will be just assigning a default value on the html field and not in the model.

Assigning default values in the model force you to create the model object first and that will set up default values for non-nullable fields like datetimes, that will make the page to show annoyings 1/1/1001 00:00:00 values in the datetime fields you could have in the rest of the model.



回答2:

The simplest way is to initialize properties in your 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 the 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 is it. Remember, you have to create a new instance of your model and pass it to the view to use it's default values. Because the view associated with this action method, expects a PersonModel instance, but when you use the create method like this:

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

the view got nothing (I mean null), so your default values aren't exists in fact.

But if you want to do this for complicated purposes, e.g. using default value as watermark, or as @JTMon said, you do not want to end-users to see default values, you will have some other solutions. Please let me know your purpose.



回答3:

Instead of using

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

Use

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


回答4:

How about defining your model to have "Default value" for its somthing property? in that case you don't need to do anything special. If that is not satisfactory (e.g. you don't want the user to see the "Default value" on the screen), you can have a custom model binder for this model that inherits from DefaultModelBinder, override just the OnModelUpdated method, in it do something along the lines of:

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

Please also note that EditorFor ignores custom html attributes that you send to it as far as I know.