操作Method的参数始终为null

时间:2017-06-02 15:55:57

标签: c# asp.net asp.net-mvc

我的控制器中的action方法参数始终为null。我在这里做错了什么?

这是与我的模型相关的代码:

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

以下是视图的代码,它填充了csv文件中存在的记录。单击提交按钮,控制器中的HttpPost操作UpdRecII。

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

以下是我的Action控制方法。我现在还没有实现方法体,因为方法参数(FileUploadIIList fileupload)总是为空(FileListII的大小始终为零)。我想代码中的某些东西阻碍了模型绑定。

我是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 个答案:

答案 0 :(得分:1)

感谢所有回复的人。 我想出了这个问题。 我更新了我的模型的代码如下,它工作!!: -

 public class FileUploadII
{
    public string roomNum { get; set; }
    public List<string> cageNums { get; set; }
    public FileUploadII()
    {
        cageNums = new List<string>();
    }
}