【问题标题】:Asp.net mvc dropdown list using ViewBag使用 ViewBag 的 Asp.net mvc 下拉列表
【发布时间】:2017-09-21 10:13:57
【问题描述】:

我在 db 中的表有字段:Id,Organisation,Info

我想制作这样的 dropdowm 列表:

<select>
  <option value='Id there'>'Organisation there'</option>...

我正在尝试使用 ViewBag:

ViewBag.Organisations = db.Organisations.ToList(); // get all fields from table using dbContext

在视图中:

@Html.DropDownList('I don`t know what i shoud write here, please help')

如何建立下拉列表?

【问题讨论】:

    标签: c# asp.net asp.net-mvc drop-down-menu


    【解决方案1】:

    您需要使用可用的constructors 之一在控制器操作中创建SelectList,并在视图中将其作为参数传递DropDownList 方法。

    在控制器中这样做:

    ViewBag.Organisations = new SelectList(db.Organisations.ToList(),"Id","Organisation");
    

    SelectList 中,我们需要在最后两个参数中指定的option 标记中指定哪个属性用作value 以及哪个属性用作text

    然后在 View 中你需要以这种方式使用它:

    @Html.DropDownList("Organization",ViewBag.Organisations as SelectList)
    

    这里第一个参数将用作select 元素的名称,第二个参数将用于填充select 中的option 元素

    以下是可用于Html.DropDownList 的重载列表:

    https://msdn.microsoft.com/en-us/library/system.web.webpages.html.htmlhelper.dropdownlist%28v=vs.111%29.aspx?f=255&MSPPError=-2147217396

    【讨论】:

    • 谢谢。请告诉我为什么我有错误 - 没有类型为 'IEnumerable' 的 ViewData 项具有键 'Organization'?
    • @Html.DropDownList("Organizations", ........) 将Organization更改为Organizations,它需要匹配ViewBag的名称,在本例中为Organizations,带有复数s.
    • @I.Bond 你还在面对这个问题吗?我展示的方式,如果你这样做,我认为你不应该看到那个错误
    【解决方案2】:

    我建议您使用 OrganisationId 属性为您的页面定义一个 ViewModel。因此,在选择组织的条目下拉列表时,将填写此值。

    @model BCO.Models.SelectOrganisationViewModel
    
    @{
        ViewBag.Title = "OrganisationInfo";
    }
    
    <div>
        @Html.DropDownListFor(o => o.OrganisationId,
            new SelectList(ViewBag.Organisations, "Id", "Name"))
    </div>
    

    SelectList 本身期望

    • 用于填充 DropDownList 的列表
    • 值(“Id”)
    • 文本(“姓名”)

    作为参数。

    【讨论】:

    • 虽然这可能会回答问题并帮助 OP,但也请尝试提供一些解释,而不仅仅是代码。
    【解决方案3】:

    您可以按如下方式使用 viewbag 中的选择列表

    @Html.DropDownListFor(m => m.Name, ViewBag.GetData as SelectList, new { @class = "form-control" })
    

    这里 ViewBag.GetData 是从控制器中填充的, 控制器代码应该是这样的

    ViewBag.GetData = new SelectList(repository.GetOrganisation(), "ID", "OraganizationName");
    

    【讨论】:

      【解决方案4】:

      控制器

      ViewBag.category = new SelectList(db.CategoryMasters.ToList(), "CategoryId", "CategoryName");
      

      查看

       <div class="form-group">
                      @Html.LabelFor(model => model.CategoryId, htmlAttributes: new { @class = "control-label col-md-2" })
                      <div class="col-md-10">
                         <div hidden>@Html.EditorFor(model => model.CategoryId, new { htmlAttributes = new { @class = "form-control", id = "categoryid" } })</div>
                              @Html.DropDownList("select", ViewBag.category as SelectList, new { @id = "category" })
                              @Html.ValidationMessageFor(model => model.CategoryId, "", new { @class = "text-danger" })
                      </div>
                  </div>
      

      JS

      <script type="text/javascript">
          $(document).ready(function () {
              $("#category").change(function () {
                  var value = $(this).val();
                  alert(+value)
                  $("#categoryid").val(value);
              });
          });
      </script>
      

      【讨论】:

        【解决方案5】:
        @model CURDInMVC.Models.StudentModel
        
        
        @Html.DropDownListFor(model => model.States,
                          new SelectList(ViewBag.States, "StateID", "StateName"))
        

        【讨论】:

        • 您好 Ramavtar Rajput,感谢您的帮助。但是,如果您能详细说明您的答案,我们将不胜感激。就像解释它是如何工作的,例如第一行在做什么,定义模型等等。您可以在此处查阅有关如何写出好的答案的 StackOverflow 指南:[stackoverflow.com/help/how-to-answer]
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-08-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-05-27
        相关资源
        最近更新 更多