我在MVC中有一個搜索函數來獲得匹配的事務。當事務被加載時,一個表被填充結果。這個結果我想發佈到一個控制器將事務導出到Excel文件。問題是,當iam將我的視圖模型發佈到我的控制器時,整個視圖模型都是空的。我已經搜尋了很多,它的一堆不同的結果,但我不能得到它的工作。這裏是我的代碼:如何將視圖中的列表傳遞給MVC控制器
修訂
視圖模型:
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/[email protected](Model.Charges[i].Reference)"><button type="button" class="m-button m-button-main">Återköp< /button></a>
}
else
{
<div> </div>
}
</td>
}
</tr>
}
}
}
</tbody>
</table>
控制器
public ActionResult ExportTransactions(TransactionsViewModel command)
{
return View();
}
在本例中未示出,但我使用的局部視圖與它周圍的MVC形式標籤的transactionstable到張貼值提供給控制器。我試圖使用for循環而不是每個循環,並試圖插入一個Html.editorFor(model => model.Charges)來獲取列表。但每次我將模型發佈到控制器,模型都是EMPTY。有什麼建議麼?
嘗試使用'for'環路使輸入名稱跟隨對象圖像'TransactionsViewModel.Charges [0] .Transaction.PhoneNumber'層次結構,並檢查是否該任何區別。 –
就像我在文中所描述的那樣,我已經嘗試過了,但是做了一些更改並且已經更新了我的問題,以便您現在可以看到我的代碼。這是沒有區別的 –
你部分視圖不會生成任何表單控件,所以沒有什麼可以回發的。由於沒有任何可編輯的東西,所以沒有形式的要點。它不清楚你想做什麼。 –