如何在MVC中添加空格下拉列表值

本文关键字:空格 下拉列表 添加 MVC | 更新日期: 2023-09-27 18:21:45

我在这样的视图中有一个下拉列表,

<style type="text/css">
.form-control {
    width: 50%;
    padding: 10px;
}

@Html.DropDownListFor(x => x.FileName, ((IEnumerable<SelectListItem>)ViewData["Items"]), new { size = 15, @class = "form-control" , @style = "padding: 10px;"})
@Html.ValidationMessageFor(x => x.FileName)

数值如下,

aaa.txt (2015-01-01) (0 B)
abcdedfff.txt (2015-02-01) (17 MB)

我想在这些项目之间添加一些空格,使其看起来像下面的

aaa.txt       (2015-01-01) (0 B)
abcdedfff.txt (2015-02-01) (17 MB)

控制器有以下代码,

if (Directory.Exists(path))
{
    files = Directory.EnumerateFiles(path, "*", SearchOption.AllDirectories)
                                     .Select(Path.GetFileName).OrderBy(f => f).ToArray();
}
long fSize = 0;
var count = 0;
var fileModified = "";
string fileSize = "";
string[] sizes = { "B", "KB", "MB", "GB" };
foreach (var filename in files)
{
   var fileInfo = new FileInfo(path + "''" + filename);
   fSize = fileInfo.Length;
   int order = 0;
   while (fSize >= 1024 && order + 1 < sizes.Length)
   {
        order++;
        fSize = fSize / 1024;
   }
   fileSize = String.Format("{0:0.##} {1}", fSize, sizes[order]);
   fileModified = fileInfo.LastWriteTime.ToString("yyyy-MM-dd hh:mm:ss tt");
   SelectListItem file = new SelectListItem() { Value = count.ToString(), Text = filename + " ( " + fileModified + " )" + " ( " + fileSize + " )" };
   fileItems.Add(file);
   count++;
}

我该怎么做?

如何在MVC中添加空格下拉列表值

要进入控制器的一个方法,需要大量代码。我会创建不同的类,也许是作为服务,为您完成所有这些工作。

我认为做到这一点的唯一方法是创建一个为您创造空间的服务。

您可以修改现有的代码来创建所需的空间,也可以创建另一个类来执行此操作。

public class SelectListService
{
   public IEnumerable<SelectListItem> GetData()
   {
       // Get your data       
       // create your spacing
       // return your formatted SelectList
   } 
}

然后在你的控制器中,你只需要…

var selectListService = new SelectListService();
fileItems = selectListService.GetData();

然后当然将它传递到您的视图中,尽管您已经在做了。

我认为您必须在集合中获得最大长度的文件名,然后使用正确的填充和空格作为文件名的填充字符。

尝试:

if (Directory.Exists(path))
        {
            files = Directory.EnumerateFiles(path, "*", SearchOption.AllDirectories)
                                             .Select(Path.GetFileName).OrderBy(f => f).ToArray();
        }
        long fSize = 0;
        var count = 0;
        var fileModified = "";
        string fileSize = "";
        string[] sizes = { "B", "KB", "MB", "GB" };
        int longest_file_length = files.Max(a => a.Length);

        foreach (var filename in files)
        {
            var fileInfo = new FileInfo(path + "''" + filename);
            fSize = fileInfo.Length;
            int order = 0;
            while (fSize >= 1024 && order + 1 < sizes.Length)
            {
                order++;
                fSize = fSize / 1024;
            }
            fileSize = String.Format("{0:0.##} {1}", fSize, sizes[order]);
            fileModified = fileInfo.LastWriteTime.ToString("yyyy-MM-dd hh:mm:ss tt");
            string padded_file_name = filename.PadRight(longest_file_length, ' ');
            SelectListItem file = new SelectListItem() { Value = count.ToString(), Text = padded_file_name + " ( " + fileModified + " )" + " ( " + fileSize + " )" };
            fileItems.Add(file);
            count++;
        }

此外,您必须使用单空格字体,以便每个字符的宽度相同。

{ font-family:"Courier New", Courier, monospace; }

您可以尝试与String.Format对齐,如下所示:

SelectListItem file = new SelectListItem() { Value = count.ToString(), Text = String.Format("{0,-25} ({1}) ({2})", filename, fileModified, fileSize) };

您可以在这个链接上看到示例,并在这里阅读更多关于字符串格式的信息。

如果您想要不可保存的输出,以下是您应该如何修改方法:

var maxFileNameLength = files.Max(x => x.Length);
foreach (var filename in files)
{
    var fileInfo = new FileInfo(path + "''" + filename);
    fSize = fileInfo.Length;
    int order = 0;
    while (fSize >= 1024 && order + 1 < sizes.Length)
    {
        order++;
        fSize = fSize / 1024;
    }
    fileSize = String.Format("{0:0.##} {1}", fSize, sizes[order]);
    fileModified = fileInfo.LastWriteTime.ToString("yyyy-MM-dd hh:mm:ss tt");
    fileNameWithSpaces = filename + string.Concat(Enumerable.Repeat("&#160;", maxFileNameLength + 1 - filename.Length));
    SelectListItem file = new SelectListItem() { Value = count.ToString(), Text = fileNameWithSpaces + " ( " + fileModified + " )" + " ( " + fileSize + " )" };
    fileItems.Add(file);
    count++;
}

好的,我检查一下。如果你想在下拉菜单中显示你的空格,你应该使用 ;而是那个空间。我更新了代码

这个怎么样

$("#selectId > option").each(function() {
var thistext=$(this).text();
var brindex=thistext.indexOf('(');
var firststring=thistext.substr(0,brindex)+"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
var laststring=thistext.substr(brindex,thistext.length);
$(this).text(firsstring+laststring);
});

未经测试,但应能正常工作