0

The action method parameter in my controller is always null. What am I doing wrong here?

This is the the code related to my Model:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;        
namespace test.Models
    {
        public class FileUploadII
        {
            public string roomNum;
            public List<string> cageNums;
            public FileUploadII()
            {
                cageNums = new List<string>();
            }
        }
    /*I'm using FileUploadIIList to pass a collection of FileUploadII objects to an action method */
        public class FileUploadIIList
        {
            public List<FileUploadII> FileListII { get; set; }//here
        }
    }

Below is the code for the view, which populates the records which were present in a csv file. On the click of the submit button the HttpPost action UpdRecII in the controller.

@{ ViewData["Title"] = "BRLCore - eCensus - File Upload Records"; } <br>
@model brlcore.webapp.Models.FileUploadIIList
<hr />
<h2>@ViewData["Title"]</h2>
<hr />
<div class="container">
  <form class="form-horizontal" method="post" asp-controller="FileUpload" asp-action="UpdRecII" enctype="multipart/form-data">
    <div class="form-group">
      <div class="col-md-4">
        <a class="btn btn-info" asp-action="Index"><span class="glyphicon glyphicon-chevron-left"></span> Back to File Upload</a>
      </div>
    </div>
    <hr />
    <div class="form-group">
      <table class="table table-striped">
        <thead>
          <tr>
            <th>Room Number</th>
            <th>Cage Number</th>
          </tr>
        </thead>
        <tbody>
          @{ 
              int i = 0; foreach (var rec in Model.FileListII.ToList())
              {

                  <tr>
                    <!--Place td recs here-->
                      <td>@rec.roomNum @Html.HiddenFor(x => x.FileListII[i].roomNum)</td>
                      <td>@{ int j = 0; @foreach (var cageNums in rec.cageNums.ToList()) {
                           <div>
                              @cageNums @Html.HiddenFor(x => x.FileListII[i].cageNums[j])
                          </div>
                      </td>
                      <td>
                          <a class="btn"><span class="glyphicon glyphicon-edit" asp-action="EditRec"></span>
                          </a>
                      </td>
              </tr>
              i++; 
              } 
          }
        </tbody>
      </table>
    </div>

    <div class="col-md-4">
      <div class="btn-group">
        <span><input class="btn btn-info" type="submit" value="Validate Details" /></span>

      </div>
    </div>
  </form>
</div>

And below is my Action control method. I've not implemented the method body as of now because, the method parameter(FileUploadIIList fileupload) is always empty (the size of FileListII is always zero). I guess something in the code hinders Model Binding.

I'm new to ASP.NET MVC.

using Microsoft.AspNetCore.Http;
using System.IO;
using brlcore.webapp.Models;
using System.Web;
using System.Net.Http;
using System.Net.Http.Headers;
using Newtonsoft.Json;

namespace test.Controllers
{
    public class FileUploadController : Controller
    { 
        [HttpPost]
        public IActionResult UpdRecII(FileUploadIIList fileupload)
        {

          //Do something later...
            return View("~/Views/FileUpload/UploadStatus.cshtml");
        }
    }
}
1
  • Haven't worked with straight html mvc bindings in a while now so I can't answer straight away but I think you would benefit from reading hanselman.com/blog/… and from looking at how the actual request is sent by the browser (in the browser dev tools) - then I'm sure you could piece it together yourself. Commented Jun 2, 2017 at 16:15

1 Answer 1

1

Thank you to all those who responded. I figured out the issue. I updated the code of my Model as follows and it worked!!:-

 public class FileUploadII
{
    public string roomNum { get; set; }
    public List<string> cageNums { get; set; }
    public FileUploadII()
    {
        cageNums = new List<string>();
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.