【问题标题】:How to show selected value in DropDownList of MVC如何在 MVC 的 DropDownList 中显示选定的值
【发布时间】:2016-04-20 12:25:10
【问题描述】:

下面是我的 DropDownList 视图

<div class="col-xs-8 col-sm-8 col-md-4">                           
 @Html.DropDownList("Status",new List<SelectListItem> { new SelectListItem{ Text="Active", Value = "Active" },new SelectListItem{ Text="InActive", Value = "InActive" }}, new { @class = "form-control" })
</div> 

来自数据库的值是“活动”或“非活动”,下拉菜单已经有这两个值。从我的数据库中,我在ViewBag.IsStatus 中赋值。 现在假设我的值是来自 DB 的“InAactive”,那么如何在 Dropdown 中将其分配为 Selected 值,而不是默认显示 First dropdown 为选中状态。

【问题讨论】:

  • 您的选项值是01(不是"Active""Inactive")所以如果属性Status 的值是1,那么第二个选项将被选中。
  • @StephenMuecke 好的,我已经更改了与 Text 相同的值。但仍然没有得到我想要的结果。
  • 您有名为Status 的模型属性吗?您应该始终使用强类型 HtmlHelper 方法 (@Html.DropDownListFor(m =&gt; m.Status, .....))
  • @Sousuke 我在视图侧绑定了我的下拉菜单,只有两个选项。

标签: asp.net-mvc dropdownlistfor


【解决方案1】:

如果您使用 MVC,最好使用 DropDownListFor。但对于您的情况,只需创建 SelectList 并将其传递给 DropDownListSelectList 构造函数对所选值有重载:

@{ //theese lines actually should be in controller.
 var list = new List<SelectListItem> 
             { 
             new SelectListItem 
                   { 
                       Text="Active", 
                       Value = "0" 
                   }
             ,new SelectListItem
                   { 
                       Text="InActive", 
                       Value = "1" 
                    }
             }
}

//thats your code
<div class="col-xs-8 col-sm-8 col-md-4">                           
   @Html.DropDownList("Status",new SelectList(list, "Value", "Text", ViewBag.IsStatus), new { @class = "form-control" })
</div> 

【讨论】:

  • 我已经在我的控制器中转移了'var list='然后我将如何在SelectList中的视图中访问该列表
  • @Steve 但是你用上面的代码来处理这个列表?
  • @Steve 在你的控制器中你应该使用ViewData.Model 来存储它。然后使用 Model 得到这个值。 或者您可以只使用ViewBag.list,然后在视图中使用ViewBag.list 访问这些数据
  • public ActionResult GetEditRecord(long Id) { using (StoreProcedureContext sc = new StoreProcedureContext()) { var list = new List&lt;SelectListItem&gt; { new SelectListItem { Text="Active", Value = "0" } ,new SelectListItem { Text="InActive", Value = "1" } }; ViewBag.List = list; var cmd = sc.EditUsers(Id); ViewBag.IsDeleted = cmd.ToList()[0].IsDeleted; return View(cmd); } }
  • 和我的观点@Html.DropDownList("Status", new SelectList(ViewBag.List, "Value", "Text"), new { @class = "form-control" }) 请告诉我哪里做错了。我希望默认选择 ViewBag.IsDeleted 值。
【解决方案2】:

如果您有一个具有 Status 属性的模型,那么只需将值分配给该属性(例如在控制器中):

型号

public class Model
{

    public string Status {get;set;}
} 

控制器

public ActionResult SomeAction()
{
    //the value has to correspond to the Value property of SelectListItems
    //that you use when you create dropdown
    //so if you have new SelectListItem{ Text="Active", Value = "Active" }
    //then the value of Status property should be 'Active' and not a 0
    var model = new Model{Status = "Active"}

    return this.View(model);
}

查看:

@model Model


@Html.DropDownListFor(m=>m.Status,new List<SelectListItem> { new SelectListItem{ Text="Active", Value = "Active" },new SelectListItem{ Text="InActive", Value = "InActive" }}, new { @class = "form-control" })

【讨论】:

  • @Html.DropDownListFor(m =&gt; m.FirstOrDefault().IsDelete, new List&lt;SelectListItem&gt; { new SelectListItem { Text = "Active", Value = "false" }, new SelectListItem { Text = "InActive", Value = "true" } }, new { @class = "form-control" })
  • isDelete 是我的 bool 属性,但我无法获取与您所说的选定值相同的值
  • 即显示列名。列名没有出现,但在写入 FirstOrDefault 之后,它显示了模型的列名
  • @Steve 实际上,您的模型看起来像是一个集合(或列表)而不是单个对象...
  • 请显示视图和相应的控制器动作代码
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-21
  • 1970-01-01
  • 2011-06-10
  • 2013-03-30
相关资源
最近更新 更多