Return 到同一页

Return to same page

我尝试上传文件。但是,如果用户没有 select 文件,则必须看到同一页面,并显示一条消息,提示您必须上传文件。

我有这个:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult UploadFile([Bind(Include = "UploadData,Directories")] /*LibraryUploadModel libraryUpload*/ UploadViewModel uploadViewModel, string designId, string folder, FormLibraryEntry formLibraryEntry, string command)
{
    //ActionResultSpecification result = SfsHelpers.GeneralHelper.HandleLibraryOverwrite(formLibraryEntry.Guid, this, command, FormRootPath, customerSchema, LibraryMode.FormLibrary);
    try
    {
        if ((uploadViewModel.UploadData != null) && (uploadViewModel.UploadData.ContentLength > 0) && !string.IsNullOrEmpty(uploadViewModel.UploadData.FileName))
        {
            var fileName = Path.GetFileName(uploadViewModel.UploadData.FileName);
            TemplateLibraryEntry entry = GetTemplateLibraryEntry(designId, customerSchema);
            var path = Path.Combine(Server.MapPath("~/"), entry.FilePath, folder.Replace('/', '\').Trim('\'), fileName);
            uploadViewModel.UploadData.SaveAs(path);
        }
        else
            return Json(new { Message = "Error in saving file, Go back and  try again" });
            // return RedirectToAction("UploadFile");
            //return View(uploadViewModel);
        }
        catch (Exception)
        {
            throw;
        }
        return RedirectToAction("Index");
    }
}

但现在显示 json 消息。您可以上传的页面名称是 UploadFile。

我现在是这样的:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult UploadFile([Bind(Include = "UploadData,Directories")] /*LibraryUploadModel libraryUpload*/ UploadViewModel uploadViewModel, string designId, string folder, FormLibraryEntry formLibraryEntry, string command)
{
    //ActionResultSpecification result = SfsHelpers.GeneralHelper.HandleLibraryOverwrite(formLibraryEntry.Guid, this, command, FormRootPath, customerSchema, LibraryMode.FormLibrary);
    try
    {
        if ((uploadViewModel.UploadData != null) && (uploadViewModel.UploadData.ContentLength > 0) && !string.IsNullOrEmpty(uploadViewModel.UploadData.FileName))
        {
            var fileName = Path.GetFileName(uploadViewModel.UploadData.FileName);
            TemplateLibraryEntry entry = GetTemplateLibraryEntry(designId, customerSchema);
            var path = Path.Combine(Server.MapPath("~/"), entry.FilePath, folder.Replace('/', '\').Trim('\'), fileName);
            uploadViewModel.UploadData.SaveAs(path);
            return RedirectToAction("Index");
        }
        else
        {
            ModelState.AddModelError("uploadViewModel", "your message");
            return View(uploadViewModel);
        }
    catch (Exception)
    {
        throw;
    }          
}

和这样的视图:

<div class="col-md-offset-2 col-md-10">
    <table>
        @for (var i = 0; i < Model.Directories.Count; i++)
        {
            <tr>
                <td>
                    <fieldset>
                        <input type="radio" name="folder" value="@Model.Directories[i]" id="folder(@i)">
                        <label for="folder(@i)">@Model.Directories[i]</label>
                    </fieldset>
                </td>
            </tr>
        }
    </table>
</div>

但问题是,如果您在没有上传文件的情况下按 Uploaden,我会收到此错误消息:

Line 55:                                 <div class="col-md-offset-2 col-md-10">
Line 56:                                     <table>
Line 57:                                         @for (var i = 0; i < Model.Directories.Count; i++)
Line 58:                                         {
Line 59:                                             <tr>

Source File: b:\Seneca\Producten\FormsServer\Trunk\SenecaFormsServer\Areas\Dashboard\Views\DesignTemplate\UploadFile.cshtml    Line: 57 

并且目录为空:Model.Directories = null

您遇到的问题是您将 Directories 绑定到您的视图,但单选按钮的名称属性格式不正确,因此不会将其发布到服务器。随后,当您将视图与 属性 将为空的模型一起发回时。在使用它的同时,也使用 LabelFor

将其更改为以下内容:

    @for (var i = 0; i < Model.Directories.Count; i++)
    {
        <tr>
            <td>
                <fieldset>
                    @Html.RadioButtonFor(m => m.Directories, Model.Directories[i])
                    @Html.LabelFor(m => m.Directories[i])
                </fieldset>
            </td>
        </tr>
    }