如何使用asp.net mvc4动态设置DropDownListFor的默认值

时间:2022-12-02 11:57:11

Dynamically i should load dropdown list and display the selected value. but dropdown loaded sucessfully but default value not selected.

动态加载下拉列表并显示所选值。但下拉成功加载但默认值未被选中。

@gt.PlantId - integer, PlantId -integer

@gt。平面-整数,平面-整数

        @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>

        }

1 个解决方案

#1


1  

If foreach loop @Html.DropDownListFor ,@Html.HidenFor, @Html.TextBoxFor or any other input element never working, because in razor input/select/textarea create a unique html name/id attribute. But using foreach it can't do this. so use for loop or EditorTemplates rather than foreach.

如果foreach循环@Html。DropDownListFor @Html。HidenFor @Html。TextBoxFor或任何其他输入元素都不会工作,因为在razor输入/select/textarea中创建一个惟一的html名称/id属性。但是使用foreach却做不到这一点。因此,使用循环或编辑器模板而不是foreach。

Other wise you can generate the html but you can't send list of item in your action.

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

Example:

例子:

Model:

模型:

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 need to exist in either Views/Shared/EditorTemplates or Views/ControllerName/EditorTemplates and name of the view (by default) should be the name of the object(Or Name of the Model) you want to use it as the template.

编辑器模板需要存在于视图/共享/编辑器模板或视图/控制器名/编辑器模板中,视图的名称(默认情况下)应该是您希望将其用作模板的对象的名称(或模型的名称)。

Editortemplates:

Editortemplates:

Views/Shared/EditorTemplates/UserEditorViewModel.cshtml

视图/共享/ 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>

View :

观点:

@model UserEditorViewModel

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

#1


1  

If foreach loop @Html.DropDownListFor ,@Html.HidenFor, @Html.TextBoxFor or any other input element never working, because in razor input/select/textarea create a unique html name/id attribute. But using foreach it can't do this. so use for loop or EditorTemplates rather than foreach.

如果foreach循环@Html。DropDownListFor @Html。HidenFor @Html。TextBoxFor或任何其他输入元素都不会工作,因为在razor输入/select/textarea中创建一个惟一的html名称/id属性。但是使用foreach却做不到这一点。因此,使用循环或编辑器模板而不是foreach。

Other wise you can generate the html but you can't send list of item in your action.

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

Example:

例子:

Model:

模型:

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 need to exist in either Views/Shared/EditorTemplates or Views/ControllerName/EditorTemplates and name of the view (by default) should be the name of the object(Or Name of the Model) you want to use it as the template.

编辑器模板需要存在于视图/共享/编辑器模板或视图/控制器名/编辑器模板中,视图的名称(默认情况下)应该是您希望将其用作模板的对象的名称(或模型的名称)。

Editortemplates:

Editortemplates:

Views/Shared/EditorTemplates/UserEditorViewModel.cshtml

视图/共享/ 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>

View :

观点:

@model UserEditorViewModel

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