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

标签 asp.net-mvc asp.net-mvc-3 forms html.textboxfor

我有一个由表单和文本框组成的 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/

相关文章:

asp.net - MVC 在运行时更新 FormsAuthenticationTicket UserData

asp.net-mvc - ASP.Net MVC 中的自定义错误页面

javascript - 自动点击/强制触发js功能

forms - 输入时没有表单标签的表单验证 - Angular 2

asp.net-mvc - 混合 http/https 站点

c# - razor 中可以使用哪些 c# 类和函数?

entity-framework - 验证复选框 - 前端 MVC3

c# - MVC3 - 获取经过身份验证的用户的部门名称

php - Codeigniter - 在数组未显示在表单编辑中时制作下拉菜单

html - 哪些元素支持::before 和::after 伪元素?