【问题标题】:Load all data to DropDownList将所有数据加载到 DropDownList
【发布时间】:2017-06-05 03:29:20
【问题描述】:

下面的代码可以正常工作。

样本输出

计算机系

技术部

产品部

假设我的部门是Product Department。我是在system / project 登录的人。可以先在 DropDownList? 中加载我的部门。我的意思是第一次加载是在加载下拉列表时我的部门设置为第一个。

控制器

ViewBag.DepartmentId = new SelectList(db.Departments, "DepartmentId", "DepartmentDesc", null).OrderBy(m => m.Text);

HTML

@Html.DropDownList("DepartmentId ", ViewData["DepartmentId "] as SelectList, "", new { @style = "width:130px;" })

【问题讨论】:

  • 在视图中传递模型之前,只需在GET方法中设置propertyDepartmentId的值,该选项就会被选中
  • @StephenMuecke 如何设置属性值?。
  • 参考this answer
  • 请注意,您不能对绑定到的属性和 SelectList 使用相同的名称(请参阅 this answer) - 将 ViewData 属性重命名为 DepartmentList
  • @StephenMuecke 只是想问问。你是怎么得到Model.TipoviDepozita的?请参阅您的第一个链接。 "NotAModelProperty", 是什么?

标签: c# html asp.net-mvc asp.net-mvc-3


【解决方案1】:

在新的 SelectList 中设置 null 的地方,设置您选择的选项。

ViewBag.DepartmentId = new SelectList(db.Departments.OrderBy(m => m.Text), "DepartmentId", "DepartmentDesc", selectedDepartmentId);

【讨论】:

    【解决方案2】:

    试试这个代码:-

    ViewBag.DepartmentId = new SelectList(db.Departments, "DepartmentId", "DepartmentDesc",SelectedDepartmentId).OrderBy(m => m.Text);
    

    还有

    @Html.DropDownList("DepartmentId ", ViewData["DepartmentId "] as SelectList, "My Department", new { @style = "width:130px;" })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-14
      相关资源
      最近更新 更多