我有一个由表单和文本框组成的 View 。 如何在每个框中为字符串和 int 值设置默认值?
我希望页面加载每个框的值,这样我就不需要键入值。
我无法更改模型中的任何内容。
@model MyDb.Production.ProductionMaterial
@{
ViewBag.Title = "CreateMaterial";
}
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>ProductionOrderMaterial</legend>
<div class="editor-label">
@Html.LabelFor(model => model.Position)
</div>
<div class="editor-field"> //something like
@Html.TextBoxFor(model => model.Position, Or 5 )
@Html.ValidationMessageFor(model => model.Position)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.ArticleId)
</div>
<div class="editor-field"> //something like
@Html.TextBoxFor(model => model.ArticleId. Or "")
@Html.ValidationMessageFor(model => model.ArticleId)
</div>
}
最佳答案
在操作中创建模型的实例,为模型的属性分配一些值并将其传递到 View
方法中。
在你的行动中有:
ProductionMaterial model = new ProductionMaterial();
model.Position = 5;
return this.View(model);
这会将模型传递给 View ,并且 TextBoxFor( model => model.Position )
将显示 5
。
关于asp.net-mvc - 如何设置默认值 HTML.TextBoxFor(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16625460/