【问题标题】:Can I use an ArrayList as a SelectList in ASP.NET MVC我可以在 ASP.NET MVC 中使用 ArrayList 作为 SelectList
【发布时间】:2016-11-30 23:41:41
【问题描述】:

我正在创建一个 ASP.NET Web 应用程序作为我学习的一部分。

目前我正在创建一个添加产品部分。我已将一些图像添加到图像文件夹中,并希望将这些图像名称添加到下拉列表中。这是我的教程提供的代码:

编辑:正如下面有人指出的,不再推荐使用 ArrayList。这就是我尝试使用这种方法的部分原因。

    public void GetImages()
    {
        try
        {
            //get all filepaths
            string[] images = Directory.GetFiles(Server.MapPath("~/Images/Products/"));

            //get all filenames and add them to an arraylist.

            ArrayList imagelist = new ArrayList();
            foreach (string image in images)
            {
                string imagename = image.Substring(image.LastIndexOf(@"\", StringComparison.Ordinal) + 1);
                imagelist.Add(imagename);
            }

         //Set the arrayList as the dropdownview's datasource and refresh
        ddlImage.DataSource = imageList;
        ddlImage.AppendDataBoundItems = true;
        ddlImage.DataBind();

    }

然后在页面加载时使用它。

当我使用 Web 表单创建它时,它可以正常工作。但是,我想为此项目使用@Html.DropDownList 操作链接。当使用脚手架连接数据库时,这些下拉列表被创建和填充得很好,我可以看到为视图生成 SelectList 的位置,即:

    // GET: Products/Create
    public ActionResult Create()
    {
        ViewBag.TypeId = new SelectList(db.ProductTypes, "Id", "Name");
        return View();
    }

我只是不确定如何将我的教程示例转换为 SelecList 初始化程序所要求的 IEnumerable。我得到的最接近的是:

     List<SelectListItem> imagelist = new List<SelectListItem>();
            foreach (string image in images)
            {
                string imagename = image.Substring(image.LastIndexOf(@"\", StringComparison.Ordinal) + 1);
                imagelist.Add(new SelectListItem() { Text = imagename });
            }

             IEnumerable<string> imager = imagelist as IEnumerable<string>;

但这似乎不对。

编辑:如下所述,我需要将值添加到新的SelectListItem

     imagelist.Add(new SelectListItem() { Text = imagename, Value = "Id" });

这似乎更好。虽然我不确定是否需要创建“imager”,但将 imageList 作为 IEnumerable。 SelectList 不是已经可以枚举了吗?

添加的问题: 另外,我应该如何将这个新列表添加到ViewBag?:

    ViewBag.TypeId = new SelectList(db.ProductTypes, "Id", "Name");
    ViewBag.TypeId = new SelectList()
    return View();     

我目前的问题是它在GetImages 方法内,我不确定如何访问它。我认为答案是超级基本的,但我对此很陌生。

任何建议将不胜感激!

再次感谢。

【问题讨论】:

  • 不要再使用 ArrayList 了,期间。为什么该代码看起来不正确?您必须在 SelectListItem 中设置一个值。
  • string imagename = image.Substring(image.LastIndexOf(@"\", StringComparison.Ordinal) + 1)代替string imagename = Path.GetFileName(image) (msdn.microsoft.com/ru-ru/library/…)
  • 好的,我到了。我理解设置值。现在我只需要将它添加到 ViewBag 中。我该怎么写? ViewBag.Image = new SelectList(?)

标签: c# asp.net asp.net-mvc visual-studio asp.net-mvc-4


【解决方案1】:
//Create a new select list. the variable Imagelist will take on whatever type SelectList.

   var Imagelist = new SelectList(
    new List<SelectListItem>
    {
        new SelectListItem { Text = imagename, Value = "Id"},
        new SelectListItem { Text = imagename2, Value = "Id2"},

    }, "Value" , "Text");


    //You can now use this viewbag in your view however you want.
      ViewBag.Image = Imagelist.

【讨论】:

    猜你喜欢
    • 2020-07-31
    • 1970-01-01
    • 1970-01-01
    • 2021-12-03
    • 2011-01-16
    • 1970-01-01
    • 2014-11-09
    • 1970-01-01
    • 2015-07-06
    相关资源
    最近更新 更多