我在这样的视图中有一个下拉列表,
<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++;
}
我该怎么做?
答案 0 :(得分:0)
这是进入Controller控制方法的很多代码。我会创建不同的类,可能作为服务,为您完成所有这些工作。
我认为唯一的方法是创建一个为您创造空间的服务。
您可以修改已有的代码来创建所需的空间,也可以创建另一个为您完成此任务的类。
public class SelectListService
{
public IEnumerable<SelectListItem> GetData()
{
// Get your data
// create your spacing
// return your formatted SelectList
}
}
然后在你的控制器中你只需要......
var selectListService = new SelectListService();
fileItems = selectListService.GetData();
然后当然将它传递到你的视图,但你已经在做了。
答案 1 :(得分:0)
如果你想要deservable输出,你应该如何修改你的方法:
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(" ", maxFileNameLength + 1 - filename.Length));
SelectListItem file = new SelectListItem() { Value = count.ToString(), Text = fileNameWithSpaces + " ( " + fileModified + " )" + " ( " + fileSize + " )" };
fileItems.Add(file);
count++;
}
好的,我检查一下。如果你想在下拉列表中展示你的空间,你应该使用那个空间。我更新了代码
答案 2 :(得分:0)
您可以尝试与String.Format
对齐,如下所示:
SelectListItem file = new SelectListItem() { Value = count.ToString(), Text = String.Format("{0,-25} ({1}) ({2})", filename, fileModified, fileSize) };
答案 3 :(得分:0)
我认为你必须在你的集合中获得文件名的最大长度,而不是使用正确的填充和空格作为文件名的填充字符。
尝试:
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; }
答案 4 :(得分:0)
这样的事情
$("#selectId > option").each(function() {
var thistext=$(this).text();
var brindex=thistext.indexOf('(');
var firststring=thistext.substr(0,brindex)+" ";
var laststring=thistext.substr(brindex,thistext.length);
$(this).text(firsstring+laststring);
});
未经测试但应该可以使用