MuM6oJuM6o
MuM6oJuM6o

Reputation: 107

The action Method's parameter is always null

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

Upvotes: 0

Views: 257

Answers (1)

MuM6oJuM6o
MuM6oJuM6o

Reputation: 107

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>();
    }
}

Upvotes: 1

Related Questions