如何设置默认值HTML.EditorFor()

时间:2022-06-17 07:57:18

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")

4 个解决方案

#1


19  

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

要在字段中显示默认值,您必须在htmlAttributes中为其指定“Value”属性,如下例所示:

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

Make sure that the V in Value is uppercase.

确保V in Value为大写。

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

这样,您只需在html字段上分配默认值,而不是在模型中。

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.

在模型中分配默认值会强制您首先创建模型对象,并为非可空字段(如日期时间)设置默认值,这将使页面显示嘈杂的1/1/1001 00:00:00值。您可以在模型的其余部分中使用的日期时间字段。

#2


11  

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:

当你想将它发送到视图时,例如在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:

这就对了。请记住,您必须创建模型的新实例并将其传递给视图以使用它的默认值。因为与此操作方法关联的视图需要PersonModel实例,但是当您使用这样的create方法时:

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

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

视图什么都没有(我的意思是null),所以你的默认值实际上并不存在。

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.

但是,如果你想为复杂的目的这样做,例如使用默认值作为水印,或者如@JTMon所说,您不希望最终用户看到默认值,您将有其他一些解决方案。请让我知道你的目的。

#3


1  

Instead of using

而不是使用

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

Use

使用

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

#4


0  

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:

如何定义模型以获得其somthing属性的“默认值”?在这种情况下,你不需要做任何特别的事情。如果这不令人满意(例如,您不希望用户在屏幕上看到“默认值”),您可以为此模型创建一个继承自DefaultModelBinder的自定义模型绑定器,仅覆盖OnModelUpdated方法,其中类似的东西:

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.

另请注意,就我所知,EditorFor会忽略您发送给它的自定义html属性。

#1


19  

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

要在字段中显示默认值,您必须在htmlAttributes中为其指定“Value”属性,如下例所示:

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

Make sure that the V in Value is uppercase.

确保V in Value为大写。

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

这样,您只需在html字段上分配默认值,而不是在模型中。

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.

在模型中分配默认值会强制您首先创建模型对象,并为非可空字段(如日期时间)设置默认值,这将使页面显示嘈杂的1/1/1001 00:00:00值。您可以在模型的其余部分中使用的日期时间字段。

#2


11  

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:

当你想将它发送到视图时,例如在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:

这就对了。请记住,您必须创建模型的新实例并将其传递给视图以使用它的默认值。因为与此操作方法关联的视图需要PersonModel实例,但是当您使用这样的create方法时:

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

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

视图什么都没有(我的意思是null),所以你的默认值实际上并不存在。

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.

但是,如果你想为复杂的目的这样做,例如使用默认值作为水印,或者如@JTMon所说,您不希望最终用户看到默认值,您将有其他一些解决方案。请让我知道你的目的。

#3


1  

Instead of using

而不是使用

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

Use

使用

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

#4


0  

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:

如何定义模型以获得其somthing属性的“默认值”?在这种情况下,你不需要做任何特别的事情。如果这不令人满意(例如,您不希望用户在屏幕上看到“默认值”),您可以为此模型创建一个继承自DefaultModelBinder的自定义模型绑定器,仅覆盖OnModelUpdated方法,其中类似的东西:

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.

另请注意,就我所知,EditorFor会忽略您发送给它的自定义html属性。