[英]Creating a listbox in a modal with files from a directory?
I am trying to grab files from a directory and I am trying to let the user select a file from a list to be later processed.我试图从目录中抓取文件,并且我试图让用户从列表中选择一个文件以供以后处理。
This is the code that gets the files.这是获取文件的代码。
[HttpGet]
public ActionResult GetFiles()
{
DirectoryInfo dinfo = new DirectoryInfo(@"C:\TestDirectory");
List<FileInfo> Files = dinfo.GetFiles("*.txt").ToList();
return PartialView("_File", Files);
}
And this is the partial view that that is going to be in the modal for now.这是现在将在模态中的部分视图。
@model List<FileInfo>
@Html.ListBoxFor(model => model, new SelectList(Model).AsEnumerable())
How I am calling the partial view from main view:我如何从主视图调用局部视图:
<div class="row">
<div class="modal fade" id="bootstrapDialog" tabindex="-1" role="dialog" aria-labelledby="myModal-label" aria-hidden="true"></div>
</div>
<div class="btn btn-default browseButton">
<span class="glyphicon glyphicon-folder-open browseDialog" data-url="@Url.Action("GetFiles","Home", null)"></span>
<span class="browseDialog" data-url="@Url.Action("GetFiles","Home", null)">Browse...</span>
</div>
@section scripts {
<script>
$(document).ready(function () {
$('.browseDialog').click(function () {
var url = $(this).data('url');
$.get(url, function (data) {
$('#bootstrapDialog').html(data);
$('#bootstrapDialog').modal('show');
});
});
});
</script>
}
The file information is being retreived but the listbox is not created.正在检索文件信息,但未创建列表框。 The listbox gives an except that the value cannot be null or empty.
列表框给出了一个除了值不能为空或为空。 Do I have to create something else that has to be passed into this listbox?
我是否必须创建其他必须传递到此列表框的内容?
And how would I allow the user to select an item to pass back to the screen behind to modal to populate a textbox?我如何允许用户选择一个项目以传递回屏幕后面的模式以填充文本框?
Both @model List<FileInfo>
and new SelectList(Model).AsEnumerable()
usage seem to be wrong, since you're not specifying both text and value to be populated in the list. @model List<FileInfo>
和new SelectList(Model).AsEnumerable()
用法似乎都是错误的,因为您没有指定要在列表中填充的文本和值。 You should create a viewmodel with setup like this:您应该使用如下设置创建一个视图模型:
public class ViewModel
{
// other properties
public string SelectedFile { get; set; }
// options list
public IEnumerable<SelectListItem> FilesList { get; set; }
}
Then, in your controller action, create List<SelectListItem>
instance from List<FileInfo>
by using Select()
extension method and picking Name
property as both option text and value:然后,在您的控制器操作中,通过使用
Select()
扩展方法并选择Name
属性作为选项文本和值,从List<FileInfo>
创建List<SelectListItem>
实例:
[HttpGet]
public ActionResult GetFiles()
{
DirectoryInfo dinfo = new DirectoryInfo(@"C:\TestDirectory");
List<FileInfo> Files = dinfo.GetFiles("*.txt").ToList();
var model = new ViewModel();
// use file name to generate option list
model.FilesList = Files.Select(x => new SelectListItem { Text = x.Name, Value = x.Name }).ToList();
return PartialView("_File", model);
}
Finally, create a DropDownListFor
helper with list populated from FilesList
property:最后,使用从
FilesList
属性填充的列表创建一个DropDownListFor
助手:
@model ViewModel
@Html.DropDownListFor(model => model.SelectedFile, Model.FilesList, ...)
Afterwards, you can retrieve the selected file by using string
property mentioned above, such like Server.MapPath()
.之后,您可以使用上述
string
属性检索所选文件,例如Server.MapPath()
。
Additional note:补充说明:
If you want to select multiple file names rather than single file name, then use IEnumerable<string>
property together with ListBoxFor
helper:如果要选择多个文件名而不是单个文件名,请使用
IEnumerable<string>
属性和ListBoxFor
助手:
ViewModel视图模型
public IEnumerable<string> SelectedFiles { get; set; }
View看法
@Html.ListBoxFor(model => model.SelectedFiles, Model.FilesList, ...)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.