如何设置默认值HTML.EditorFor() [英] how to set default value HTML.EditorFor()
问题描述
如何设置一个默认值Im我editorboxes,这样的情况下,我不写东西
在框中它不发送空。
how can i set a default value im my editorboxes so that in case i dont write anything in the box it dont send null.
<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")
推荐答案
要在您必须htmlAttributes分配'值'属性为它就像这个例子一字段中显示的默认值
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 } })
确认,在值在V是大写的。
Make sure that the V in Value is uppercase.
此方式,您将它分配上的HTML领域,而不是在模型中的默认值。
This way you will be just assigning a default value on the html field and not in the model.
在模型迫使你分配的缺省值先创建模型对象,这将设置默认值不可为空的字段,如日期时间,这将使页面显示annoyings 1001年1月1日零时00分00秒在日期时间字段的值,你可以在模型的其余部分。
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.
这篇关于如何设置默认值HTML.EditorFor()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!