我尝试在视图中创建目录列表。我的模特是:
public class UploadModel
{
[Required]
[Display(Name = "FormToUpload", ResourceType = typeof(Resources.Entity.Form))]
public HttpPostedFileBase UploadData { get; set; }
public string[] Directories { get; set; }
}
这是控制器:
[HttpGet]
public ActionResult UploadFile(string designId)
{
UploadModel model = new UploadModel();
string customerSchema = SfsHelpers.StateHelper.GetSchema();
TemplateLibraryEntry entry = GetTemplateLibraryEntry(designId, customerSchema);
var path = Path.Combine(Server.MapPath("~/"), entry.FilePath);
model.Directories = Directory.GetFiles(path, "*", SearchOption.AllDirectories);
return View(model);
}
这是我的观点:
<div class="form-group">
@foreach (var item in Model.Directories )
{
@Html.CheckBoxFor(model => true, item)
@Html.LabelFor(model => model.Directories)
}
但是我收到了这个错误:
模板只能用于字段访问,属性访问,单维数组索引或单参数自定义索引器表达式。
谢谢
我现在就这样:
public class UploadViewModel
{
private UploadModel _uploadModel;
public string[] Directories { get; set; }
public UploadViewModel(UploadModel uploadModel)
{
_uploadModel = uploadModel;
}
}
public class UploadModel
{
[Required]
[Display(Name = "FormToUpload", ResourceType = typeof(Resources.Entity.Form))]
public HttpPostedFileBase UploadData { get; set; }
public string[] Directories { get; set; }
}
在视图中我使用新的UploadViewModel:@model SenecaFormsServer.Areas.Dashboard.Models.UploadViewModel
和复选框:
<div class="form-group">
@foreach (var item in Model.Directories )
{
@Html.CheckBoxFor(model => true, item)
@Html.LabelFor(model => model.Directories)
}
</div>
和我的上传方法:
[HttpGet]
public ActionResult UploadFile(string designId,UploadModel model)
{
UploadModel model = new UploadModel();
UploadViewModel uploadViewModel = new UploadViewModel(model);
string customerSchema = SfsHelpers.StateHelper.GetSchema();
TemplateLibraryEntry entry = GetTemplateLibraryEntry(designId, customerSchema);
var path = Path.Combine(Server.MapPath("~/"), entry.FilePath);
//model.Directories = Directory.GetFiles(path, "*", SearchOption.AllDirectories);
uploadViewModel.Directories = Directory.GetFiles(path, "*", SearchOption.AllDirectories);
return View(uploadViewModel);
}
但我仍然收到错误:
模板只能用于字段访问,属性访问,单维数组索引或单参数自定义索引器表达式。
如果我尝试这个观点:
<div class="form-group">
@foreach (var item in Model.Directories )
{
@*@Html.CheckBoxFor(model => true, item)*@
@Html.CheckBoxFor(m => m.Directories, new { @checked = "checked" });
@Html.LabelFor(model => model.Directories)
}
</div>
我收到此错误: 无法将类型'string []'隐式转换为'bool'