如何将视图中的列表传递给MVC控制器

时间:2015-11-07 18:16:47

标签: c# asp.net-mvc

我在MVC中有一个搜索功能来获取匹配的事务。加载事务时,将使用结果填充表。此结果我想发布到控制器以将事务导出到Excel文件。问题是,当我将我的视图模型发布到我的控制器时,整个视图模型都是空的。我已经搜索了很多,它有一些不同的结果,但我不能让它工作。这是我的代码:

已更新

查看型号:

    public class TransactionsViewModel : ViewModelBase
    {
        public TransactionsViewModel()
        {
            Charges = new List<Transaction>();
            Invoices = new List<Invoices>();
        }

        public List<Transaction> Charges { get; set; }
        public List<Invoices> Invoices { get; set; }

    [DisplayName("Telefonnummer")]
    public string PhoneNumber { get; set; }
    [DisplayName("Personnummer")]
    public string PersonalIdentityNumber { get; set; }

    [DisplayName("Ordernummer")]
    public string OrderNumber { get; set; }

    [DisplayName("Orderbeskrivning")]
    public string OrderDescription { get; set; }

    // A lot of other input format controls

public class Transaction
    {
            public string Status { get; set; }
            public DateTime Date { get; set; }
            public string Msisdn { get; set; }
            public string OrderNo { get; set; }
            public string NetsId { get; set; }
            public string Reference { get; set; }
            public string Message { get; set; }
            public decimal Amount { get; set; }
            public decimal Vat { get; set; }
            public string Merchant { get; set; }
            public decimal Fee { get; set; }
            public string PaymentType { get; set; }
            public string OrderNumber { get; set; }
            public string OrderDescription { get; set; }
    }
    }

查看

    <div id="transactionsdiv">
    @using (Html.BeginForm("SearchTransactions", "Transactions", FormMethod.Get, new { @class = "merchant-form" }))
    {
        <div class="left-col">
            <div class="form-group">
                @Html.LabelFor(model => model.PhoneNumber)
                @Html.EditorFor(model => model.PhoneNumber, new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.PhoneNumber, "", new { @class = "text-danger" })
            </div>

        </div>
        <div class="form-button">
            <input type="submit" value="Sök" class="btn btn-primary" />
        </div>
    }


<div>
    @Html.Partial("ExportFiles")
</div>

ExportFiles

<div id="">
@using (Html.BeginForm("ExportTransactions", "Transactions", FormMethod.Post, new { @class = "merchant-form" }))
{
    <input type="submit" value="Exportera" class="btn btn-primary" />
    if (Model.Charges != null)
    {
        for (int i = 0; i < Model.Charges.Count; i++)
        {
            <input type="hidden" name="command.Transaction[i].Date" value="@(Model.Charges[i].Date)" />
            <input type="hidden" name="command.Transaction[i].Status" value="@(Model.Charges[i].Status)" />
            <input type="hidden" name="command.Transaction[i].Msisdn" value="@(Model.Charges[i].Msisdn)" />
            <input type="hidden" name="command.Transaction[i].OrderNo" value="@(Model.Charges[i].OrderNo)"/>
            <input type="hidden" name="command.Transaction[i].NetsId" value="@(Model.Charges[i].NetsId)"/>
            <input type="hidden" name="command.Transaction[i].Message" value="@(Model.Charges[i].Message)"/>
            <input type="hidden" name="command.Transaction[i].Amount" value="@(Model.Charges[i].Amount)"/>
            <input type="hidden" name="command.Transaction[i].Reference" value="@(Model.Charges[i].Reference)" />

        }
    }
}
<table id="Transaction" class="table table-striped table-bordered" width="100%" cellspacing="0" style="display: none;">
    <thead>
        <tr>
            <th>Datum</th>
            <th>Status</th>
            <th class="amount-col">Msisdn</th>
            <th>OrderNo</th>
            <th>Netsreferens</th>
            <th>Beskrivning</th>
            <th class="amount-col">Belopp</th>
            @if (Model.AllowRefund)
            {
                <th>Återköp</th>
            }
        </tr>
    </thead>
    <tbody>
        @if (Model.Charges != null)
        {
            for (int i = 0; i < Model.Charges.Count; i++)
            {
                <tr>
                    <td class="text-col">@Model.Charges[i].Date</td>
                    <td>@Model.Charges[i].Status</td>
                    <td class="amount-col">@Model.Charges[i].Msisdn</td>
                    <td>@Model.Charges[i].OrderNo</td>
                    <td>@Model.Charges[i].NetsId</td>
                    <td>@Model.Charges[i].Message</td>
                    <td class="amount-col">@Model.Charges[i].Amount</td>
                    @if (Model.AllowRefund)
                    {
                        <td>
                            @if (Model.Charges[i].Status == "Completed")
                    {
                                <a href="/Merchants/Refund?chargeId=@(Model.Charges[i].Reference)"><button type="button" class="m-button m-button-main">Återköp<    /button></a>
                            }
                            else
                            {
                                <div>&nbsp;</div>
                            }
                        </td>
                    }
                </tr>
            }

        }
        }

    </tbody>
</table>

控制器

public ActionResult ExportTransactions(TransactionsViewModel command)
    {
        return View();
    }

在这个示例中没有显示,但是我使用了一个部分视图来查找transactionstable,并在其周围添加了一个mvc表单标记,以将这些值发布到控制器。我试图使用for循环而不是每个循环,并尝试插入一个Html.editorFor(model =&gt; model.Charges)来获取列表。但每次我将模型发布到控制器时,模型都是EMPTY。有什么建议吗?

1 个答案:

答案 0 :(得分:1)

当您提交表单时,它将使用表单内输入控件的值填充请求表单数据。在您的表单中没有输入控件,因此不会发送任何内容。

向服务器发送一个完整的数据列表,你刚才从中获得了很多开销。我只是发回搜索参数,服务器可以再次执行搜索并返回您需要构建的Excel。或者,如果您的搜索速度太慢而无法再次执行,那么在数据库中查找的费用(或发票)的列表就足够了。

在任何情况下,您需要发送回服务器(作为表单参数)必须在表单中。最简单的方法是为需要发回的每个字段添加隐藏输入。在这种情况下,您将发送一个对象数组,因此您必须输入create proper names

这样的事情可以解决问题:

<强> ExportFiles

<div id="">
@using (Html.BeginForm("ExportTransactions", "Transactions", FormMethod.Post, new {@class = "merchant-form"}))
{
    <input type="submit" value="Exportera" class="btn btn-primary"/>
    @if (Model.Charges != null)
    {
        for (int i = 0; i < Model.Charges.Count; i++)
        {
            <input type="hidden" name="command.Charges[@(i)].Date" value="@Model.Charges[i].Date" />
            <input type="hidden" name="command.Charges[@(i)].Status" value="@Model.Charges[i].Status" />
            <input type="hidden" name="command.Charges[@(i)].Msisdn" value="@Model.Charges[i].Msisdn" />
            <input type="hidden" name="command.Charges[@(i)].OrderNo" value="@Model.Charges[i].OrderNo" />

            @* All the other properties of your objects that you need to send to the server on post. You get the idea... *@
        }
    }
}
<table id="Transaction" class="table table-striped table-bordered" width="100%" cellspacing="0" style="display: none;">
    <thead>
    <tr>
        <th>Datum</th>
        <th>Status</th>
        <th class="amount-col">Msisdn</th>
        <th>OrderNo</th>
        <th>Netsreferens</th>
        <th>Beskrivning</th>
        <th class="amount-col">Belopp</th>
        @if (Model.AllowRefund)
        {
            <th>Återköp</th>
        }
    </tr>
    </thead>
    <tbody>
    @if (Model.Charges != null)
    {
        for (int i = 0; i < Model.Charges.Count; i++)
        {
            <tr>
                <td class="text-col">@Model.Charges[i].Date</td>
                <td>@Model.Charges[i].Status</td>
                <td class="amount-col">@Model.Charges[i].Msisdn</td>
                <td>@Model.Charges[i].OrderNo</td>
                <td>@Model.Charges[i].NetsId</td>
                <td>@Model.Charges[i].Message</td>
                <td class="amount-col">@Model.Charges[i].Amount</td>
                @if (Model.AllowRefund)
                {
                    <td>
                        @if (Model.Charges[i].Status == "Completed")
                        {
                            <a href="/Merchants/Refund?chargeId=@(Model.Charges[i].Reference)"><button type="button" class="m-button m-button-main">Återköp<    /button></a>
                        }
                        else
                        {
                            <div>&nbsp;</div>
                        }
                    </td>
                }
            </tr>
        }

    }
    }

    </tbody>
</table>

显示数据的表与将发布到服务器的表单无关,因此在它之外完全没问题。

请记住,隐藏的输入只是隐藏在用户输入的输入,但它们不是只读的,因此任何具有足够知识的用户都可以在发送到服务器之前更改它们,并且您将生成包含不正确数据的Excel。因此,我强烈建议您在创建Excel之前使用原始参数再次执行搜索。