从视图传递到控制器时,列表返回null

时间:2018-07-31 13:53:21

标签: asp.net asp.net-mvc entity-framework

我的ActionResult中有一个Controller,它使用存储过程从数据库中检索数据。

RIROController.cs

public ActionResult myRIRO()
{
    // Calls the main ViewModel
    var vm = new ResourceViewModel();

    string EID = Session["EID"].ToString();


    vm.CheckValues = db_RIRO.sp_GetAllCheckStatus()
    .Select(a => new SelectListItem
    {
        Value = a.CheckID.ToString(),
        Text = a.CheckStatus
    })
    .ToList();

    // Calls the dictionary RIROList is nested inside ResourceViewModel 
    vm.UserRIList = new List<RIROFormViewModel>();
            // Set current user's value of completion status to ddl
    sp_GetUserRIForm_Result RIUserChkValue = db_RIRO.sp_GetUserRIForm(EID).FirstOrDefault();
    sp_GetUserROForm_Result ROUserChkValue = db_RIRO.sp_GetUserROForm(EID).FirstOrDefault();

    vm.RICheckID = RIUserChkValue.CheckID;
    vm.ROCheckID = ROUserChkValue.CheckID;

    foreach (var item in db_RIRO.sp_GetUserRIForm(Session["EID"].ToString()))
    {
        vm.UserRIList.Add(new RIROFormViewModel()
        {
            ItemNo = item.ItemNo,
            CheckItem = item.CheckItem,
            EmployeeTypeID = item.EmployeeTypeID,
            ItemID = item.ItemID,
            CheckID = item.CheckID,
            DateCompleted = item.DateCompleted,
            RIComment = item.Comments

        });
    }
   return View(vm);
}

查看/myRIRO.cshtml

@model OnlineRIRO.ViewModel.ResourceViewModel

@using (Html.BeginForm("UpdateRIROForm", "RIRO", FormMethod.Post, new { @class = "form-horizontal" }))
{
    @foreach (var item in Model.UserRIList)
    {
        <tr>
            <td>@item.ItemNo</td>
            <td>@Html.Raw(item.CheckItem)</td>
            <td>
                @Html.DropDownListFor(a => a.RICheckID, Model.CheckValues, new { @class = "form-control input-sm" })
            </td>
            <td>
                @Html.TextBoxFor(a => item.RIComment, new { @class = "form-control input-sm" })
            </td>
        </tr>
    }
}

我想检索vm.UserRIList内部的数据,并对该集合中的每个项目执行存储过程。

UpdateRIROForm

[HttpPost]
public ActionResult UpdateRIROForm(ResourceViewModel riroInfo)
{
    var EID = Session["EID"].ToString();

    if (ModelState.IsValid)
    {
        try
        {
            foreach (var item in riroInfo.UserRIList)
            {
              db_RIRO.sp_UpdateUserRIRODraft(EID, item.ItemID, item.CheckID, item.RIComment)
              db_RIRO.SaveChanges();
            }

            return RedirectToAction("myRIRO");
        }
        catch
        {
            return RedirectToAction("myRIRO");
        }
    }
    else
    {
        TempData["Fail"] = 1;
        return RedirectToAction("myRIRO");
    }

}

当我调试UpdateRIROForm时,riroInfo.UserRIList为空。我的代码中缺少任何内容吗?

1 个答案:

答案 0 :(得分:2)

在视图中使用for循环进行索引

@model OnlineRIRO.ViewModel.ResourceViewModel

@using (Html.BeginForm("UpdateRIROForm", "RIRO", FormMethod.Post, new { @class = "form-horizontal" })) {
    @for(int index = 0, index < Model.UserRIList.Count, index++) {
        <tr>
            <td>@Model.UserRIList[index].ItemNo</td>
            <td>@Html.Raw(Model.UserRIList[index].CheckItem)</td>
            <td>
                @Html.DropDownListFor(model => model.RICheckID, Model.CheckValues, new { @class = "form-control input-sm" })
            </td>
            <td>
                @Html.TextBoxFor(model => model.UserRIList[index].RIComment, new { @class = "form-control input-sm" })
            </td>
        </tr>
    }
}

这将允许在将表单回发到控制器时填充模型中的集合。