返回同一页面

时间:2015-06-12 14:25:10

标签: c# asp.net-mvc entity-framework-4

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

我有这个:

[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 

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

1 个答案:

答案 0 :(得分:0)

您遇到的问题是您将Directories绑定到视图,但单选按钮的名称属性未正确形成,因此无法发布到服务器。随后,当您使用模型发回视图时,该属性将为null。如果您正在使用它,请同时使用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>
    }