将模型发布回控制器:一个属性始终为null

时间:2015-04-14 19:52:48

标签: asp.net-mvc

我是.NET Web开发的新手(尝试),尝试从 WinForms 更改为MVC 5。我将模型对象的一部分发布回控制器时遇到问题。

我最初将模型发送到视图:

[HttpPost]
public ActionResult LookupWorkOrder(string workOrderNumber)
{
    var workOrder = getDirector().getWorkOrderInfo(workOrderNumber);

    if (workOrder == null)
    {
        Response.StatusDescription = string.Format("Invalid Work Order Number: {0}", workOrderNumber);

        return View("Error");
    }

    var thing = new LotAddModel() { workOrder = workOrder, quantity = 7 };

    return View("FoundWorkOrder", thing);
}

以下是您需要了解的两个模型:

public class LotAddModel
{
    public WorkOrderInfo workOrder { get; set; }
    public int employeeId { get; set; }
    public string lotNumber { get; set; }
    public int quantity { get; set; }
}

public class WorkOrderInfo
{
    public string workOrderNumber { get; set; }
    public string salesOrderNumber { get; set; }
    public string itemNumber { get; set; }
    public string description { get; set; }
    public decimal quantity { get; set; }
    public string status { get; set; }

    public List<WorkOrderLotNumber> lots { get; set; }
    public List<WorkOrderPuller> pullers { get; set; }
    public List<WorkOrderBuilder> builders { get; set; }
    public List<WorkOrderTester> testers { get; set; }
    public List<WorkOrderInspector> inspectors { get; set; }

    #region cTor

    public WorkOrderInfo()
    {
        // I don't want to do anything here
    }

    internal WorkOrderInfo(string workOrderNumber, MainWorkOrderInfo mainInfo, List<WorkOrderLotNumber> lots,
            List<WorkOrderPuller> pullers, List<WorkOrderBuilder> builders,
            List<WorkOrderTester> testers, List<WorkOrderInspector> inspectors)
    {
        this.workOrderNumber = workOrderNumber;
        this.salesOrderNumber = mainInfo.salesOrderNumber;
        this.itemNumber = mainInfo.itemNumber;
        this.description = mainInfo.description;
        this.quantity = mainInfo.quantity;
        this.status = mainInfo.status;

        this.lots = lots;
        this.pullers = pullers;
        this.builders = builders;
        this.testers = testers;
        this.inspectors = inspectors;
    }

    #endregion
}

一切都非常精彩地进入了视野。 thing.workOrder对象中的属性显示完全正常。

然而,当我尝试将该模型发送回Controller时:

@using (Html.BeginForm("AddLotNumber", "WorkOrderLotEntry"))
{
    @Html.HiddenFor(x => x.workOrder);
    @Html.HiddenFor(x => x.quantity); // just did this to test something

    <label for="employeeId">EmployeeID:</label>
    <input type="text" name="employeeId" ? />

    <label for="lotNumber">Lot Number:</label>
    <input type="text" name="lotNumber" />

    <input type="submit" value="Add Lot Number" />
}

使用此控制器方法:

[HttpPost]
public ActionResult AddLotNumber(LotAddModel model)
{
    /*
     *  model.quantity is equal to 7, just like it should be.
     *  model.workOrder is NULL --> THIS IS THE REASON I AM POSTING THIS QUESTION - why is it always null??
     *  
     */
    try
    {
        getDirector().addLotNumber(model.workOrder, model.employeeId, model.lotNumber, model.quantity);

        return View("FoundWorkOrder", model.workOrder);
    }
    catch (Exception exception)
    {
        Response.StatusDescription = exception.Message;

        return View("Error"); // I get this bad boy every time...
    }
}

除了model属性之外,所有内容都完美地加载到model.workOrder对象中,而{{1}}属性始终为null。

我不知道为什么我无法将该workOrder属性发送回Controller。我真的希望能够做到这一点,或至少类似的东西。

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

model.workOrder

WorkOrderInfo类型的属性,@Html.HiddenFor(x => x.workOrder);不知道如何序列化它。打开chrome dev工具并检查隐藏字段中的内容。这将被发布回控制器,如果无法将其反序列化回WorkOrderInfo,您将获得null。为什么不做@Html.HiddenFor(x => x.workOrder.workOrderNumber)之类的事情? 然后改变

public class LotAddModel
{
    public string workOrderNumber{ get; set; }
    public int employeeId { get; set; }
    public string lotNumber { get; set; }
    public int quantity { get; set; }
}

修改

public class WorkOrderInfo
{
    public string workOrderNumber { get; set; }
    public string salesOrderNumber { get; set; }
    public string itemNumber { get; set; }
    public string description { get; set; }
    public decimal quantity { get; set; }
    public string status { get; set; }

    public List<WorkOrderLotNumber> lots { get; set; }
    public List<WorkOrderPuller> pullers { get; set; }
    public List<WorkOrderBuilder> builders { get; set; }
    public List<WorkOrderTester> testers { get; set; }
    public List<WorkOrderInspector> inspectors { get; set; }

    #region cTor

    public WorkOrderInfo()
    {
        // I don't want to do anything here
    }

    internal WorkOrderInfo(string workOrderNumber, MainWorkOrderInfo mainInfo, List<WorkOrderLotNumber> lots,
            List<WorkOrderPuller> pullers, List<WorkOrderBuilder> builders,
            List<WorkOrderTester> testers, List<WorkOrderInspector> inspectors)
    {
        this.workOrderNumber = workOrderNumber;
        this.salesOrderNumber = mainInfo.salesOrderNumber;
        this.itemNumber = mainInfo.itemNumber;
        this.description = mainInfo.description;
        this.quantity = mainInfo.quantity;
        this.status = mainInfo.status;

        this.lots = lots;
        this.pullers = pullers;
        this.builders = builders;
        this.testers = testers;
        this.inspectors = inspectors;
    }

    public override string ToString()
    {
         return JsonConvert.SerializeObject(this);
    }
    #endregion
}