John
John

Reputation: 3296

Binding to a view model with a nested list of type class

I'm trying to bind to a view model for a POST request into my controller action, and it appears the correct parameters are being sent, but my viewmodels data seems to be null despite trying multiple things. I have the following view model that I'm binding to from within my view:

public class OrderDetailViewModel {
   public List < OrderDetail > OrderDetails;
}

The data loads into my views properly, here's what the main view looks like:

<form class="form-horizontal" id="update-orderdetail" action="/orderdetail/update" method="post">
        @Html.AntiForgeryToken()
        @Html.ValidationSummary(true, "", new { @class = "alert alert-danger" })
        <table class="table table-striped table-bordered table-hover results">
            <thead>
                ... tr tags ...
            </thead>
            <tbody>
                @Html.EditorFor(x => x.OrderDetails)
            </tbody>
        </table>

        <div class="form-group">
            <div class="pull-right">
                <button type="reset" class="btn btn-default">Reset</button>
                <button type="submit" class="btn btn-success">Save</button>
            </div>
        </div>
    </form>

Here's what my EditorTemplates/OrderDetail.cshtml looks like:

@model CorinthVendorPortal.Models.OrderDetail
<tr>
    <td>@Html.DisplayFor(x => x.PurchaseOrder)</td>
    <td class="editable">
        @Html.HiddenFor(x => x.ID)
        @Html.HiddenFor(x => x.PurchaseOrder)
        @Html.TextBoxFor(x => x.Price, new { @type = "number", @class = "form-control" })
    </td>
    <td>@Html.DisplayFor(x => x.StockCode)</td>
    <td>@Html.DisplayFor(x => x.MStockDes)</td>
    <td>@Html.DisplayFor(x => x.OrderQtyOrig)</td>
    <td class="editable">
        @Html.TextBoxFor(x => x.OrderQtyCur, new { @type = "number", @class = "form-control" })
    </td>
    <td>@Html.DisplayFor(x => x.DueDateOrig)</td>
    <td class="editable">
        @Html.TextBoxFor(x => x.DueDateCur, new { @type = "date", @class = "form-control" })
    </td>
    <td>@Html.DisplayFor(x => x.ShipDateOrig)</td>
    <td class="editable">
        @Html.TextBoxFor(x => x.ShipDateCur, new { @type = "date", @class = "form-control" })
    </td>
</tr>

Here's what the OrderDetail controller's Update method looks like (in which my model's OrderDetails property is null):

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Update(OrderDetailViewModel model) {
    var l = model; // model.OrderDetails is always null
    ....
    return null;
}

Here are the parameters that are being POSTed:

OrderDetails[0].ID:1
OrderDetails[0].PurchaseOrder:0246298
OrderDetails[0].Price:8.85000
OrderDetails[0].OrderQtyCur:200.000000
OrderDetails[0].DueDateCur:
OrderDetails[0].ShipDateCur:
OrderDetails[1].ID:2
OrderDetails[1].PurchaseOrder:0246298
OrderDetails[1].Price:1.40000
OrderDetails[1].OrderQtyCur:750.000000
OrderDetails[1].DueDateCur:
OrderDetails[1].ShipDateCur:

Any help or advice would be appreciated.

Upvotes: 1

Views: 1014

Answers (1)

dotnetom
dotnetom

Reputation: 24901

You are using field in your view model, but MVC binding works only with properties.

Change

public class OrderDetailViewModel {
   public List<OrderDetail> OrderDetails;
}

to

public class OrderDetailViewModel {
   public List <OrderDetail> OrderDetails { get; set; }
}

and your code should start working.

Upvotes: 3

Related Questions