【问题标题】:How to set dynamically default value for DropDownListFor using asp.net mvc4如何使用asp.net mvc4为DropDownListFor动态设置默认值
【发布时间】:2017-10-12 22:47:08
【问题描述】:

我应该动态加载下拉列表并显示所选值。但下拉菜单加载成功,但未选择默认值。

@gt.PlantId - 整数, PlantId - 整数

        @foreach (var gt in Model.RoleList)
        {
            <tr>
                <td>@Html.DropDownListFor(Model => Model.Plants,new SelectList(Model.Plants,"PlantId","PlantName", @gt.PlantId))</td>
                <td>@gt.PlantId</td>
                <td>@gt.RoleId</td>
                @using (Ajax.BeginForm("deletedet", new AjaxOptions() { UpdateTargetId = "Edit-User", AllowCache = true, InsertionMode = InsertionMode.ReplaceWith }))
                {
                    @Html.Hidden("userId", @gt.UserId)

                <td><p data-placement="top" data-toggle="tooltip" title="Delete"><button class="btn btn-danger btn-xs" data-title="Delete" data-toggle="modal" data-target="#myTable"><span class="glyphicon glyphicon-trash"></span></button></p></td>
                }
            </tr>

        }

【问题讨论】:

  • @Html.DropDownListFor(m =&gt; m.Genre, new SelectList(Model.Genres, "Id", "Name"), "DEFAULT VALUE GOES HERE", new { @class = "form-control" })
  • 尝试上述方法并进行相应的更改。 "DEAFULT VALUE GOES HERE" 将其替换为您想要的默认值
  • 您好坚不可摧,非常感谢。基于值我必须显示文本。当我在这个我的 id 中输入“DEFAULT VALUE GOES HERE”时,我收到了错误,比如无法转换为字符串
  • &lt;td&gt;@Html.DropDownListFor(Model =&gt; Model.Plants,new SelectList(Model.Plants,"PlantId","PlantName", Model.Plants))&lt;/td&gt;
  • 成功了吗...?

标签: asp.net-mvc asp.net-mvc-4 c#-4.0 razor


【解决方案1】:

如果 foreach 循环 @Html.DropDownListFor ,@Html.HidenFor, @Html.TextBoxFor 或任何其他输入元素无法正常工作, 因为在 razor input/select/textarea 创建一个独特的 html name/id 属性。但是使用foreach 它不能做到这一点。 所以使用 for 循环或 EditorTemplates 而不是 foreach。

否则,您可以生成 html,但不能在您的操作中发送项目列表。

示例:

型号:

public class UserEditorViewModel
    {
       public string UserId { get; set; }
        public string RoleId { get; set; }
        public string UserName { get; set; }
        public IEnumerable<Roles> Roles { get; set; }
    }

EditorTemplates 需要存在于Views/Shared/EditorTemplatesViews/ControllerName/EditorTemplates 中,并且视图的名称(默认情况下)应该是您要用作模板的object(Or Name of the Model) 的名称。

编辑器模板:

Views/Shared/EditorTemplates/UserEditorViewModel.cshtml

@model UserEditorViewModel

<div class="form-group">
    @Html.DisplayNameFor(m => m.UserId)
    @Html.EditorFor(m => m.UserId)
</div>
<div class="form-group">
    @Html.DisplayNameFor(m => m.UserName)
    @Html.EditorFor(m => m.UserName)
</div>
<div class="form-group">
    @Html.DisplayNameFor(m => m.RoleId)
    @Html.DropDownListFor(m => m.RoleId, new SelectList(Model.Roles,"RoleId","RoleName",Model.RoleId))
</div>

查看:

@model UserEditorViewModel

@using (Html.BeginForm("--Your Action--", "--Your Controller--"))//Or use Ajax.BeginForm if you need
{
    @Html.EditorForModel()
    <input type="submit" value="Save" />
}

【讨论】:

  • 朋友您好,非常感谢。它工作正常还有一个问题是我想保存所有更改的下拉列表值。例如一个用户它将来多个角色和部门下拉列表我想保存所有下拉值。你能帮帮我吗?
  • 简单您的操作收到类似ActionName(List&lt;UserEditorViewModel&gt; model) .@SENA
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-18
  • 1970-01-01
  • 2016-04-25
  • 1970-01-01
相关资源
最近更新 更多