Return to same page

159 views Asked by At

I try to upload a file. But if the user doesnt select a file, the same page has to been seen with a message that you have to upload a file.

I have this:

[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");
    }
}

But now the json message is shown. The name of the page where you can upload is UploadFile.

I have it now like this:

[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;
    }          
}

and the view like this:

<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>

but the problem is that if you press on Uploaden without an upload file, I get this error:

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 

and the directories are null: Model.Directories = null

1

There are 1 answers

2
Mathew Thompson On BEST ANSWER

The problem you have is that you are binding your Directories to your view, but the name attribute of your radio button isn't correctly formed so it won't be posted to the server. Subsequently, when you send the view back with the model that property will be null. Whilst you're at it, use LabelFor as well.

Change it to the following:

    @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>
    }