asp.net-mvc 如何设置默认值 HTML.EditorFor()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16561889/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 02:49:44  来源:igfitidea点击:

how to set default value HTML.EditorFor()

asp.net-mvcrazoreditorfor

提问by Fadi Alkadi

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

回答by Gabriel Pravaz

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 是大写的。

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您可以在模型的其余部分中使用日期时间字段。

回答by ravy amiry

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.Createaction-method:

当你想将它发送到视图时,例如在PersonController.Createaction-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 PersonModelinstance, 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 所说,您不希望最终用户看到默认值,您将有一些其他解决方案。请让我知道你的目的。

回答by Prabhat

Instead of using

而不是使用

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

Use

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

回答by JTMon

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:

如何定义您的模型为其某些属性具有“默认值”?在这种情况下,您不需要做任何特别的事情。如果这不令人满意(例如,您不希望用户在屏幕上看到“默认值”),您可以为该模型创建一个自定义模型绑定器,该绑定器继承自 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 属性。