【问题标题】:How to add extra item in asp .net mvc dropdownlist from controller如何从控制器在 asp .net mvc 下拉列表中添加额外的项目
【发布时间】:2016-06-09 07:31:02
【问题描述】:

我正在使用 ViewModel 在我的 asp .net MVC 应用程序中创建视图。在这个视图中,我需要绑定一个下拉列表(成功绑定)。 问题:我想在下拉列表中添加具有值的额外项目,例如“select”、“0”

这是我的视图模型:

 public class PagesViewModel
{
    public int Id { get; set; }

    [Required]
    [Display(Name = "Page Name")]
    public string PageName { get; set; }


    [Display(Name = "Parent Page")]
    public IEnumerable<Page> Page { get; set; }
}

我的控制器:

 public ActionResult Create()
    {
        var model = new PagesViewModel
        {
            Page = db.Pages.Where(s => s.IsActive == true).OrderBy(r => r.PageName)

        };
        //--- Here I need to add extra items manually
        return View(model);
    }

我的观点:

  @Html.DropDownListFor(model => model.Id, new SelectList(Model.Page, "Id", "PageName"), "Select Parent Page", htmlAttributes: new { @class = "form-control" })

【问题讨论】:

    标签: asp.net-mvc


    【解决方案1】:

    DropDownListFor 助手有一个overload,它允许您指定一个选项标签——我假设这就是您所追求的。

    @Html.DropDownListFor(m => m.SomeID, Model.SomeSelectList, "Select one...", new { @class="your-class-here" })
    

    这将呈现一个带有value="" 属性的&lt;option&gt; 元素:

    <select id="SomeId" name="SomeId" class="your-class-here">
        <option value="">Select one...</option>
        <!-- ... the rest of the items from the SelectList -->
    </select>
    

    否则,您的 Pages 属性必须是从 ICollection 派生的东西(如列表),这将是 allow you to add items

    型号:

    public class PagesViewModel
    {
        public int Id { get; set; }
    
        [Required]
        [Display(Name = "Page Name")]
        public string PageName { get; set; }
    
    
        [Display(Name = "Parent Page")]
        public ICollection<Page> Page { get; set; }
    }
    

    控制器:

    public ActionResult Create()
    {
        var model = new PagesViewModel
        {
            Page = db.Pages.Where(s => s.IsActive == true)
                .OrderBy(r => r.PageName)
                .ToList()
        };
    
        model.Pages.Add(new Page{ /* ... object initializers here */ });
    
        return View(model);
    }
    

    【讨论】:

    • T 我们可以在特定位置添加新项目吗?
    • 我相信有一个 Insert 方法,但我无法很快验证。
    • 是的,有插入方法,但我无法使用。请帮忙。我已经尝试过了,但它给了我错误: Page = db.Pages.Where(s => s.IsActive == true).OrderBy(r => r.PageName).ToList().Insert(0, new Page { Id = 0, PageName = "父页面" })
    • 目前我真的不能给你一个详细的答复,但看起来你正试图将一条记录插入 DbSet - 这是行不通的。将模型中的集合类型更改为 IList,这将使您可以访问 Insert msdn.microsoft.com/en-us/library/8zsfbxz8(v=vs.110).aspx
    • 完成:model.Page.Insert(0, new Page { Id = 0, PageName = "Text here" });
    【解决方案2】:

    试试这个

         var model = new PagesViewModel
                {
                    Page = db.Pages.Where(s => s.IsActive == true).OrderBy(r => r.PageName)
    
                };
    model.Page.Add(new Page{ Name='ABC' ... }) // You can define your properties here
    

    【讨论】:

    • .Add 不可用。
    【解决方案3】:
    public ActionResult Create()
        {
            var model = new PagesViewModel
            {
                Page = db.Pages.Where(s => s.IsActive == true).OrderBy(r => r.PageName)
    
            };
    List<SelectListItem> listItem = new List<SelectListItem>();
    foreach(var item in Model)
    {
        listItem.Add(new SelectListItem{Text = item.PageName, Value=item.Id});
    }
    listItem.Add( new SelectListItem{Text = "SomeText", Value="Some Value"});
            //--- Here I need to add extra items manually
            return View(model);
        }
    

    您还可以通过 linq 扩展方法 Insert(index, ValueToBeInserted) 在顶部插入可选文本。如果要在顶部插入,索引将为零。

    如果有帮助,请标记为分析器。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-01
      • 2017-02-20
      相关资源
      最近更新 更多