代码之家  ›  专栏  ›  技术社区  ›  c3f

使用ViewBag中的值创建不可编辑的窗体控件

  •  0
  • c3f  · 技术社区  · 5 年前

    我在“Create.cshtml”视图中工作。我正在尝试使用存储在 ViewBag . 类似于:

    @Html.Textbox(
        *model attribute*,
        *default value ie. (ViewBag.ID)*,
        htmlAttributes: new { @class = "form-control" @readonly="readonly" }
    )
    

    我还没有弄清楚正确的语法,甚至我应该使用什么样的控件。这是控件的工作版本,但没有预设值,并且是可编辑的。

    @using (Html.BeginForm())
    {
       @Html.AntiForgeryToken()
       <div class ="form-horizontal">
          <div class="form-group">
             @Html.EditorFor(model => model.LeagueID, new { htmlAttributes = new { @class = "form-control" } })
          </div>
       </div>
    }
    
    1 回复  |  直到 5 年前
        1
  •  0
  •   MorenajeRD    5 年前

    你应该尝试:

    @Html.EditorFor(model => model.LeagueID, new { htmlAttributes = new { @class = "form-control", disabled = true,value= ViewBag.LeagueId } })