如何从视图传递一个列表到MVC控制器 [英] How to pass a list from view to a MVC controller

查看:174
本文介绍了如何从视图传递一个列表到MVC控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在MVC中的搜索功能来获得相匹配的交易。当交易被加载的表填充了结果。这样的结果我想张贴到控制器的交易导出到Excel文件。问题是,当IAM张贴我的视图模型来我的控制器整个视图模型为空。我已搜查周围很多和它的一堆不同的结果,但我不能得到它的工作。这里是我的code:

I have a search function in MVC to get matching transactions. When the transactions is loaded a table is populated with the result. This result i want to post to a controller to export the transactions to an Excel file. The problem is when iam posting my view model to my controller the whole view model is empty. I have searched around a lot and its a bunch of different result but i cant get it to work. Here is my code:

更新时间:

视图模型:

    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();
    }

本例中没有显示,但我使用与它周围的MVC表单标签的transactionstable的局部视图后,价值观到控制器。我曾尝试使用一个for循环,而不是为每个循环,并试图刚刚插入Html.editorFor(型号=> model.Charges),以获取列表。但每次我发布模型到控制器的型号是空的。有什么建议?

Not showing in this example but i am using a partial view for the transactionstable with a mvc form tag around it to post that values to the controller. I have tried to use a for loop instead a for each loop and tried just to insert a Html.editorFor(model => model.Charges) to get the list. But everytime i post the model to the controller the model is EMPTY. Any suggestions?

推荐答案

当您提交表单,它将填充表单内输入控件的值请求表单数据。在你的形式有没有输入控件所以没有被发送。

When you submit a form, it will fill the request form data with the values of the input controls inside the form. In your form there are no input controls so nothing is sent.

发送回服务器,你刚刚从它似乎是一个很大的开销,以我的数据的完整列表。我也只是发回的搜索参数和服务器可以再次执行搜索,返回你需要建立的Excel中。或者,如果你的搜索速度太慢,再次执行它,收费的ID(或发票)在数据库中查找列表应该够了。

Sending back to the server a whole list of data that you just got from it seems like a lot of overhead to me. I would just send back the search parameters and the server can perform the search again and return the Excel you need to build. Or, if your search is too slow to perform it again, a list of ids of the charges (or invoices) to lookup in the database should be enough.

在任何情况下,你需要发送回服务器(如表单参数)都必须在表单中。最简单的方法是添加一个隐藏的输入每个需要发回现场。在这种情况下您发送对象的数组,所以你将不得不<一个href=\"http://www.hanselman.com/blog/ASPNETWireFormatForModelBindingToArraysListsCollectionsDictionaries.aspx\"相对=nofollow> 的投入创造适当的名称。

In any case, what you need to send back to the server (as form parameters) have to be in the form. The easiest way is to add a hidden input for each field you need to send back. In this case you are sending an array of objects so you will have to create proper names of the inputs.

这样的事情应该做的伎俩:

Something like this should do the trick:

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>

这显示了数据表有没有关系将被发送到服务器的形式,因此是它完美的罚款之外。

The table that shows the data has nothing to do with the form that will be posted to the server so it is perfectly fine outside of it.

请该隐藏的输入是正义的,输入用户隐藏,但他们不是只读所以用足够的知识,任何用户都可以发送到服务器之前改变他们,你会产生一个excel用不正确的数据。正因为如此,我会强烈建议再次执行搜索创建Excel中之前,使用原始参数

Keep in mind that the hidden inputs are just that, inputs hidden from the user, but they are not readonly so any user with enough knowledge can change them before sending to the server and you will produce an excel with incorrect data. Because of this, I will strongly recommend performing the search again using the original parameters before creating the Excel.

这篇关于如何从视图传递一个列表到MVC控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆