在子文件夹中创建文件夹

时间:2015-06-17 07:57:11

标签: c# asp.net-mvc

我想在子文件夹中创建一个文件夹。我可以创建一个新文件夹。但不是在现有的文件夹中。我有单选按钮,您可以在其中选择要添加新文件夹的文件夹。

我有这个方法:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult CreateDirectory(CreateDirectoryModel model)
{
    try {
        //string validationError = null;
        string customerSchema = SfsHelpers.StateHelper.GetSchema();
        TemplateLibraryEntry entry = GetTemplateLibraryEntry(model.DesignId, customerSchema);
        bool CreateFolder = (model.Name != null);
        string folder = model.Directories[model.SelectedFolderId]; //Create folder in folder
        var path = Path.Combine(Server.MapPath("~/"), entry.FilePath) + folder +  "\\" + model.Name;
        if (Directory.Exists(path)) {                    
            ViewBag.Message = Resources.Entity.DesignTemplate.DirectoryExists;
            return View(model);
        }
        if (ModelState.IsValid) {
            if (model.Name != null) {


                if (!Directory.Exists(path)) {
                    DirectoryInfo di = Directory.CreateDirectory(path);
                }
            }
        }
    }
    catch (Exception ex) {
        Logger.Current.LogMessage(LogType.Warning, StateHelper.GetSchema(), LogCategory.Sfs, "Unable to create folder: {0}", ex.ToString());
        AddDelayedNotification(Resources.Entity.DesignTemplate.FileCouldNotBeUploaded, Notification.NotificationType.Error);
    }

    return RedirectToAction(model.DesignId, "DesignTemplate/File");
}

这是观点:

 <div class="well no-padding">
    <div class="bg-color-white">
        <div class="form-group">
            @Html.LabelFor(model => model.Directories, new { @class = "text-bold control-label col-md-2 col-sm-2" })
            <div class="col-lg-6 col-md-8 col-sm-10">
                @for (var i = 0; i < Model.Directories.Count; i++) {
                    <div>
                        @Html.RadioButtonFor(model => model.SelectedFolderId, i, new { id = "folder" + i })
                        <label for="folder@(i)">@Model.Directories[i]</label>
                    </div>
                }
            </div>
        </div>

        <div class="col-xs-12">
        </div>
        <div class="form-group">
            @Html.LabelFor(model => model.Name, new { @class = "text-bold control-label col-md-2" })
            <div class="col-lg-6 col-md-8 col-sm-10 ">
                @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Name)

                @if (ViewBag.Message != null) {
                    <p style="color:#b94a48;">@ViewBag.Message</p>
                }
            </div>
        </div>                   

    </div>
</div>

问题在于:string folder = model.Directories[model.SelectedFolderId];

目录为空。但是SelectedFolderId是正确的

我添加了:CreateUploadFileFolders(model, model.DesignId);

用这种方法:

 public void CreateUploadFileFolders(CreateDirectoryModel model, string designId)
        {
            string customerSchema = SfsHelpers.StateHelper.GetSchema();
            TemplateLibraryEntry entry = GetTemplateLibraryEntry(designId, customerSchema);
            var path = Path.Combine(Server.MapPath("~/"), entry.FilePath);
            model.DesignId = designId;
            model.Directories = new List<string>();
            model.Directories.Add("/");
            model.Directories.AddRange(Directory.GetDirectories(path, "*", SearchOption.AllDirectories));
            for (int i = 1; i < model.Directories.Count; i++) {
                model.Directories[i] = model.Directories[i].Substring(path.Length).Replace('\\', '/');
            }
            model.Directories.Sort();
        }

0 个答案:

没有答案