1

我有一个看起来像这样的 Razor 视图:

@model Namespace.Namespace.SupplierInvoiceMatchingVm

@using(Html.BeginForm("MatchLines", "PaymentTransaction"))
{
    <table class="dataTable" style="width: 95%; margin: 0px auto;">
    <tr>
        <th></th>
        <th>PO Line</th> 
        <th>Description</th> 
    </tr>
    @for (var i = 0; i < Model.Lines.Count; i++)
    {
        <tr>
            <td>@Html.CheckBoxFor(x => x.Lines[i].Selected)</td>
            <td>@Html.DisplayFor(x =>x.Lines[i].LineRef) @Html.HiddenFor(x => x.Lines[i].LineRef)</td>
            <td>@Html.DisplayFor(x =>x.Lines[i].Description) @Html.HiddenFor(x => x.Lines[i].Description)</td>
        </tr>
    }

    </table>
    <input type="submit" value="Submit"/>
}

对象Lines列表在哪里,方法签名看起来像SupplierInvoiceMatchingDtoMatchLines

public ActionResult MatchLines(IEnumerable<SupplierInvoiceMatchingDto> list)

当我点击此视图上的提交按钮时,列表以null.

但是,如果我将 更改ModelList<SupplierInvoiceMatchingDto>,并将所有表行更改为x => x[i].Whatever,它会发布所有信息。

我的问题是:我如何让它将列表发布到控制器,同时保留模型,SupplierInvoiceMatchingVm因为我需要在这个视图中从模型中获取一些其他东西(为了简洁起见,我已经取出了)。

注意:我删除了一些用户输入字段,它不仅仅是发布与给出的相同数据。

4

2 回答 2

2

You could use the [Bind] attribute and specify a prefix:

[HttpPost]
public ActionResult MatchLines([Bind(Prefix="Lines")] IEnumerable<SupplierInvoiceMatchingDto> list)
{
    ...
}

or even better use a view model:

public class MatchLinesViewModel
{
    public List<SupplierInvoiceMatchingDto> Lines { get; set; }
}

and then have your POST controller action take this view model:

[HttpPost]
public ActionResult MatchLines(MatchLinesViewModel model)
{
    ... model.Lines will obviously contain the required information
}
于 2013-05-29T14:20:21.377 回答
1

您的Post操作未正确接收模型(应该是您的 ViewModel)?不应该是:

[HttpPost]
public ActionResult MatchLines(SupplierInvoiceMatchingVm viewModel)
{
    var list = viewModel.Lines;
    // ...
}
于 2013-05-29T14:22:27.567 回答