GlutVonSmark
GlutVonSmark

Reputation: 322

How to pass a complex model back to the controller in asp.net mvc

New to web development. I have a view that allows user to select an excel file. When submit "preview" button is pressed file is read and data is sent back to the user to preview the data. Then I want to be able send the model back to the control for db upload. (this is the part I'm struggling with).

ViewModel:

public class UploadItemsViewModel
{
    public List<Item> Items { get; set; }

    public int CompanyID { get; set; }
    public Company Company { get; set; }

    public HttpPostedFileBase upload { get; set; }

    public UploadJournalsViewModel()
    {
        Items = new List<Item>();
    }

}

Controller:

public ActionResult Upload(FormCollection formCollection, int CompanyID)
    {
        if (Request != null)
        {
            HttpPostedFileBase file = Request.Files["UploadedFile"];
            if ((file != null) && (file.ContentLength > 0) && !string.IsNullOrEmpty(file.FileName))
            {
                string fileName = file.FileName;
                string fileContentType = file.ContentType;
                byte[] fileBytes = new byte[file.ContentLength];
                var data = file.InputStream.Read(fileBytes, 0, Convert.ToInt32(file.ContentLength));
            }
        }
        UploadItemsViewModel itmViewModel = new UploadItemsViewModel { Company = db.Companies.Find(CompanyID), CompanyID = CompanyID };
        return View(itmViewModel);
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Upload(UploadItemsViewModel itmViewModel, string Preview, string Upload)
    {
        if (ModelState.IsValid)
        {
            if (itmViewModel.upload != null && itmViewModel.upload.ContentLength >0)
            {
                try
                {
                    itmlViewModel.Items = App.Services.ItemsMassUploadFileRead.ReadExcelFile(itmViewModel.upload, db.Companies.Find(itmViewModel.CompanyID));

                    if (string.IsNullOrEmpty(Preview))
                    {
                        foreach (var itm in itmViewModel.Items)
                        {
                            itm.StartDate = DateTime.Today;
                            itm.CompanyID = itmViewModel.CompanyID;
                            itm.User = null;
                            itm.Items.Add(itm);
                            db.SaveChanges();
                        }
                        return View();
                    }
                    else
                    {
                        return View(itmViewModel);
                    }

                   }                    }
                catch (Exception ex)
                {
                    ModelState.AddModelError("File", ex.Message.ToString());
                    return View(itmViewModel);
                }
            }
            else
            {
                ModelState.AddModelError("File", "Please Upload Your file");
            }
        }
        return View(itmViewModel);
    }

View:

 @using (Html.BeginForm("Upload", "ItemsUpload", null, FormMethod.Post,  new { enctype = "multipart/form-data" }))

{@Html.AntiForgeryToken() @Html.HiddenFor(model => model.CompanyID)

<div class="form-group">
    <div class="input-group">
        <label class="input-group-btn">
            <span class="btn btn-default">
                Browse&hellip; <input type="file" style="display: none;" accept=".xlsx" name="upload">
            </span>
        </label>
        <input type="text" class="form-control " readonly>
    </div>
    <span class="help-block">
        Please use a provided Excel template
    </span>
</div>
<div class="form-group">
    <input type="submit" value="Preview" name ="Preview" class="btn btn-default" disabled style="display: none" id="submit"/>
</div>
<div class="form-group">
    <input type="submit" value="Upload" name="Upload" class="btn btn-default" id="Upload" />
</div>

<div class="help-block" id="previewHelp" style="display: none">
    Preview results and scroll down to upload data to the database.
</div>



if (Model.Journals.Count != 0)
{
  table here to preview the upload
}

After clicking the Upload button model comes back without the "items" collection.

Upvotes: 0

Views: 525

Answers (1)

Majdi Saibi
Majdi Saibi

Reputation: 435

The Items list will be always null in the controller, because you don't rendered any input on the View with the name Items

Upvotes: 2

Related Questions