0

我需要组合框中的图片,但我只有路径

var files = Url.Content("~/Content/skin/Simple.png");

@(Html.Telerik().ComboBox()
      .Name("ComboBox")
      .BindTo(files.Select(f => new SelectListItem { Selected = false, Text = f.ToString(), Value = "" }))
   )

谢谢,但如果我需要显示文件夹中的所有图片

string searchFolder = Url.Content("~/Content/skin/");
var filters = new[] { "jpg", "jpeg", "png", "gif", "tiff", "bmp" };
var files = Some Class.GetFilesFrom(searchFolder, filters, false);

有我的完整代码

控制器

public ActionResult Combo()
    {
      return View();
    }

模型

public class Combo
  {
    public static string[] GetFilesFrom(string searchFolder, string[] filters, bool isRecursive)
    {
      var filesFound = new List<String>();
      var searchOption = isRecursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly;
      foreach (var filter in filters)
      {
        filesFound.AddRange(Directory.GetFiles(searchFolder, String.Format("*.{0}", filter), searchOption));
      }
      return filesFound.ToArray();
    }
  }

和查看

@using TETRA.RS.WWW.UI.Models

@{
  string searchFolder = Url.Content("~/Content/skin/");
  var filters = new[] { "jpg", "jpeg", "png", "gif", "tiff", "bmp" };
  var files = Combo.GetFilesFrom(searchFolder, filters, false);
}


@(Html.Telerik().ComboBox()
      .Name("ComboBox")
      .BindTo(files.Select(f => new SelectListItem { Selected = false, Text = f, Value = "" }))
)

我在组合框中只看到完整路径而且我不知道一个文件夹中有多少张图片

4

2 回答 2

1

有多种方法可以将图像添加到可从 radcombobox 中选择的项目,每种方法都有其优点,最适合的只能由您的要求定义。

最简单的方法是为 ImageUrl 属性分配一个值。(Telerik 文档:http ://www.telerik.com/help/aspnet-ajax/combobox-appearance-item-images.html )

或者,您可以修改项目模板。(Telerik 演示:http ://demos.telerik.com/aspnet-ajax/combobox/examples/functionality/templates/defaultcs.aspx )

于 2013-09-16T14:37:39.590 回答
0

我使用过 Kendo UI 我有一个包含图片的文件夹,但我不知道需要多少才能获取所有图片并在组合框中使用

方法查找所有图片

public static string[] GetFilesFrom(string searchFolder, string[] filters, bool isRecursive)
  {
    var filesFound = new List<string>();
    var searchOption = isRecursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly;
    foreach (var filter in filters)
    {
      filesFound.AddRange(Directory.GetFiles(searchFolder, String.Format("*.{0}", filter), searchOption));
    }
    return filesFound.Select(Path.GetFileName).ToArray();
  }

设置控制器

public ActionResult Theme()
    {
      return View();
    }

    public JsonResult ThemeJson()
    {
      string searchFolder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Content\\themes\\skin");
      var filters = new[] { "jpg", "jpeg", "png", "gif", "tiff", "bmp" };
      var files = Tools.GetFilesFrom(searchFolder, filters, false);
      return Json(files.Select(f => new { Id = f, Name = Path.GetFileNameWithoutExtension(f) }));
    }

和查看

@using Kendo.Mvc.UI

@(Html.Kendo().ComboBox()
          .Name("customers")
          .DataTextField("Name")
          .DataValueField("Id")
          .HtmlAttributes(new { })
          .DataSource(source => source.Read(read => read.Action("ThemeJson", "Settings").Type(HttpVerbs.Post)))
          .Filter("startswith")
          .Height(300)
          .Template("<img src=\"" + Url.Content("~/Content/themes/skin/") + "${data.Id}\" alt=\"${data.Id}\" />" +
                        "<dl>" +
                        "Name theme: ${ data.Name }" +
                        "</dl>")
)

您还需要添加参考 kendo.all.min.js 和 Kendo.Mvc.UI

于 2013-09-19T13:48:24.260 回答