使用for循环时,在发布到MVC操作时,对象列表为null

时间:2015-09-03 18:56:07

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

我已经在过去很好地向MVC动作发布了一个对象列表,但我不确定为什么我会在这种情况下获得null结果。

我创建了一个自定义视图模型:

public class CustomPmScheduleGenerator
{
    public bool Save { get; set; }
    public bool Search { get; set; }
    public DateTime? BegDueDate { get; set; }
    public DateTime? EndDateDate { get; set; }
    public string GenSn { get; set; }
    public string PmName { get; set; }
    public string Type { get; set; }
    public IList<stp_GetPmSchedulesGenerator_Result> Pms { get; set; }
    public IList<stp_GetRepairSchedulesGenerator_Result> Repairs { get; set; } 
}

以上对象是我发布到索引操作的内容。 Pms和Repairs都返回null。

这是我的行动(GET和POST):

    [HttpGet]
    public ActionResult Index()
    {
        var model = new CustomPmScheduleGenerator();

        var pms = _db.stp_GetPmSchedulesGenerator(null, null, Convert.ToInt32(Session["CustomerNumber"]), null, null).ToList();
        var repairs = _db.stp_GetRepairSchedulesGenerator(null, null, Convert.ToInt32(Session["CustomerNumber"]), null, null).ToList();

        model.Pms = pms;
        model.Repairs = repairs;

        return View(model);
    }

    [HttpPost]
    public ActionResult Index(CustomPmScheduleGenerator model)
    {
        if (model.Save)
        {
            //Update the PMs
            foreach (var pm in model.Pms)
            {
                //TODO: Update the current PM
            }

            //Update the Repairs
            foreach (var repair in model.Repairs)
            {
                //TODO: Update the Current Repair
            }
        }
        else if (model.Search)
        {
            var pms = _db.stp_GetPmSchedulesGenerator(model.BegDueDate, model.EndDateDate, Convert.ToInt32(Session["CustomerNumber"]),model.PmName, model.GenSn).ToList();

            var repairs = _db.stp_GetRepairSchedulesGenerator(null, null, Convert.ToInt32(Session["CustomerNumber"]), null, null).ToList();

            model.Pms = pms;
            model.Repairs = repairs;

            return View(model);
        }

        return View();
    }

我在if语句行的POST操作上设置了一个断点,并检查参数&#34; model&#34;的值。这就是我看到空值的地方。

以下是我的观点:

@using (Html.BeginForm("Index", "PmScheduleGenerator", FormMethod.Post))
{
    <div>
        <h1>
            <strong>PM Schedule</strong>
            @Html.ActionLink("Add Repair", "AddRepair", null, new { @class = "btn btn-warning", @style = "float:right;margin-left:5px;" })
            @Html.ActionLink("Save", "AddRepair", null, new { @class = "btn btn-success", @style = "float:right" })
        </h1>
    </div>
    <hr />


    <div class="row">
        <div class="col-lg-2 form-group">
            <div class="editor-label">
                @Html.Label("Due Beg. Date")
            </div>
            <div class="editor-field">
                <div class="input-group date">
                    @Html.TextBoxFor(model => model.BegDueDate, null, new { @class = "form-control" })
                    <span class="input-group-addon"><i class="glyphicon glyphicon-th"></i></span>
                </div>
            </div>
        </div>
        <div class="col-lg-2 form-group">
            <div class="editor-label">
                @Html.Label("Due End Date")
            </div>
            <div class="editor-field">
                <div class="input-group date">
                    @Html.TextBoxFor(model => model.EndDateDate, null, new { @class = "form-control" })
                    <span class="input-group-addon"><i class="glyphicon glyphicon-th"></i></span>
                </div>
            </div>
        </div>
        <div class="col-lg-2 form-group">
            <div class="editor-label">
                @Html.Label("Gen S/N")
            </div>
            <div class="editor-field">
                <div class="input-group">
                    @Html.DropDownListFor(model => model.GenSn, DropdownLists.GetGenSns(Convert.ToInt32(Session["CustomerNumber"]), Session["PmType"].ToString(), Session["Whse"].ToString()), new { @class = "form-control" })
                </div>
            </div>
        </div>
        <div class="col-lg-2 form-group">
            <div class="editor-label">
                @Html.Label("PM Name")
            </div>
            <div class="editor-field">
                <div class="input-group">
                    @Html.DropDownListFor(model => model.PmName, DropdownLists.GetPmNames(Convert.ToInt32(Session["CustomerNumber"]), Session["PmType"].ToString(), Session["Whse"].ToString()), new { @class = "form-control" })
                </div>
            </div>
        </div>
        <div class="col-lg-2 form-group">
            <div class="editor-label">
                @Html.Label("Type")
            </div>
            <div class="editor-field">
                <div class="input-group">
                    @Html.DropDownListFor(model => model.Type, DropdownLists.GetTypes(), new { @class = "form-control" })
                </div>
            </div>
        </div>
        <div class="col-lg-2 form-group">
            <div class="editor-label">
                &nbsp;
            </div>
            <div class="editor-field">
                <input type="submit" value="Search" class="btn btn-success" />
                <input type="submit" value="Clear" class="btn btn-danger" />
            </div>
        </div>
    </div>

    <table class="table table-striped table-bordered">
        <thead>
            <tr>
                <th>Gen S/N</th>
                <th>Loc ID</th>
                <th style="width: 140px;">PM Description</th>
                <th>City</th>
                <th>Next PM Due Dt.</th>
                <th>Sched Arrival Dt.</th>
                <th style="width: 160px;">Actions</th>
            </tr>
        </thead>
        <tbody>
            @for (var i = 0; i < Model.Pms.Count; i++)
            {
                <tr>
                    <td>
                        @Html.HiddenFor(x => x.Pms[i].FleetTruckNo)
                        @Html.DisplayFor(x => x.Pms[i].FleetTruckNo)
                    </td>
                    <td>
                        @Html.HiddenFor(x => x.Pms[i].LocationID)
                        @Html.DisplayFor(x => x.Pms[i].LocationID)
                    </td>
                    <td>
                        @Html.HiddenFor(x => x.Pms[i].Description)
                        @Html.DisplayFor(x => x.Pms[i].Description)
                    </td>
                    <td>
                        @Html.HiddenFor(x => x.Pms[i].City)
                        @Html.DisplayFor(x => x.Pms[i].City)
                    </td>
                    <td>
                        <div class="editor-field">
                            <div class="input-group date">
                                @Html.TextBoxFor(x => x.Pms[i].NextPMDueDt, "{0:MM/dd/yyyy}", new { @class = "form-control" })
                                <span class="input-group-addon"><i class="glyphicon glyphicon-th"></i></span>
                            </div>
                        </div>
                    </td>
                    <td>
                        <div class="editor-field">
                            <div class="input-group date">
                                @Html.TextBoxFor(x => x.Pms[i].EstArrivalDt, "{0:MM/dd/yyyy}", new { @class = "form-control" })
                                <span class="input-group-addon"><i class="glyphicon glyphicon-th"></i></span>
                            </div>
                        </div>
                    </td>
                    <td>
                        <input type="hidden" class="query" value="?customerNumber=@Model.Pms[i].CustNo&fleetTruckNo=@Model.Pms[i].FleetTruckNo&sro=@Model.Pms[i].RepairOrdNo&scheduleId=@Model.Pms[i].ScheduleID" />
                        <input type="button" class="comments btn btn-primary" value="Comments" />
                        <input type="button" class="btn btn-success" value="Process" />
                    </td>
                </tr>

            }
            @for (var i = 0; i < Model.Repairs.Count; i++)
            {
                <tr>
                    <td>
                        @Html.HiddenFor(x => x.Repairs[i].FleetTruckNo)
                        @Html.DisplayFor(x => x.Repairs[i].FleetTruckNo)
                    </td>
                    <td>
                        @Html.HiddenFor(x => x.Repairs[i].LocationID)
                        @Html.DisplayFor(x => x.Repairs[i].LocationID)
                    </td>
                    <td>
                        @Html.HiddenFor(x => x.Repairs[i].Description)
                        @Html.DisplayFor(x => x.Repairs[i].Description)
                    </td>
                    <td>
                        @Html.HiddenFor(x => x.Repairs[i].City)
                        @Html.DisplayFor(x => x.Repairs[i].City)
                    </td>
                    <td>
                        N/A
                    </td>
                    <td>
                        <div class="editor-field">
                            <div class="input-group date">
                                @Html.TextBoxFor(x => x.Repairs[i].ScheduledDt, "{0:MM/dd/yyyy}", new { @class = "form-control" })
                                <span class="input-group-addon"><i class="glyphicon glyphicon-th"></i></span>
                            </div>
                        </div>
                    </td>
                    <td>
                        <input type="hidden" class="query" value="?customerNumber=@Model.Repairs[i].CustNo&fleetTruckNo=@Model.Repairs[i].FleetTruckNo&sro=@Model.Repairs[i].SRO&scheduleId=@Model.Repairs[i].ScheduleID&repair=true" />
                        <input type="button" class="comments btn btn-primary" value="Comments" />
                        <input type="button" class="btn btn-success" value="Process" />
                    </td>
                </tr>

            }
        </tbody>
    </table>
}

正如你在视图中看到的那样,我有两个for循环去抛修和PM。有谁知道为什么我在POST动作中获得空值?

2 个答案:

答案 0 :(得分:1)

将此添加到模型构造函数中,看看它是否有帮助。

public class CustomPmScheduleGenerator
{
    public CustomPmScheduleGenerator()
    {
        Pms = new List<stp_GetPmSchedulesGenerator_Result>();
        Repairs = new List<stp_GetRepairSchedulesGenerator_Result>();
    }

    public bool Save { get; set; }
    etc..
    public IList<stp_GetPmSchedulesGenerator_Result> Pms { get; set; }
    public IList<stp_GetRepairSchedulesGenerator_Result> Repairs { get; set; } 
}

模型绑定器可以创建模型的新实例,但Pms和Repairs对象为null,除非在构造函数中实例化它们

答案 1 :(得分:-1)

for 置于 if 语句

Task<ServiceStatus>.FromResult(null)