2015-11-07 152 views
0

我在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>&nbsp;</div> 
          } 
         </td> 
        } 
       </tr> 
      } 

     } 
     } 

    </tbody> 
</table> 

控制器

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

在本例中未示出,但我使用的局部視圖與它周圍的MVC形式標籤的transactionstable到張貼值提供給控制器。我試圖使用for循環而不是每個循環,並試圖插入一個Html.editorFor(model => model.Charges)來獲取列表。但每次我將模型發佈到控制器,模型都是EMPTY。有什麼建議麼?

+1

嘗試使用'for'環路使輸入名稱跟隨對象圖像'TransactionsViewModel.Charges [0] .Transaction.PhoneNumber'層次結構,並檢查是否該任何區別。 –

+0

就像我在文中所描述的那樣,我已經嘗試過了,但是做了一些更改並且已經更新了我的問題,以便您現在可以看到我的代碼。這是沒有區別的 –

+1

你部分視圖不會生成任何表單控件,所以沒有什麼可以回發的。由於沒有任何可編輯的東西,所以沒有形式的要點。它不清楚你想做什麼。 –

回答

1

當您提交表單時,它將填寫表單中輸入控件值的請求表單數據。在您的表單中沒有輸入控件,因此不會發送任何內容。

向服務器發送一個剛剛從中獲得的整個數據列表,似乎對我來說有很多開銷。我只是發回搜索參數,服務器可以再次執行搜索並返回您需要構建的Excel。或者,如果您的搜索速度太慢而無法再執行一次,那麼在數據庫中查找費用(或發票)的ID列表就足夠了。

在任何情況下,你需要發送回服務器(作爲表單參數)的形式。最簡單的方法是爲每個需要發回的字段添加一個隱藏的輸入。在這種情況下,您發送的是一組對象,因此您必須輸入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/[email protected](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之前使用原始參數再次執行搜索。

+0

Okey,所以我做了你所建議的改變,我試圖讓一個數組作爲輸入參數類型來控制發佈參數。但是當控制器方法被擊中時,數組是空的。我已經閱讀過你建議並嘗試過的文章,但是我無法弄清楚什麼是錯誤的。是否它是一個局部視圖,並且Transactionspartialview發佈到Transactionviewmodel類型的控制器?對不起,如果我混淆你! –

+0

@AndreasJangefalk敬畏的男人我知道你的嘗試,但我只是不認爲你得到它,但不要擔心,我在那裏有兩個,無論如何是有幫助的...當我這樣做的東西...我從控制器中的「FormCollection窗體」中使用.....這有助於理解控制器能夠拾取的內容。所以使用「公共ActionResult ExportTransactions(TransactionsViewModel命令,FormCollection窗體)」並檢查窗體,當你調整你的代碼。那裏的其他人提供了很好的建議。 – Seabizkit

+0

有點遲,因爲我剛剛閱讀,你去尋求解決方案。當然,更好的解決方案。但是從您在更新後的問題中顯示的代碼中,我看到您正在將隱藏輸入的名稱指定爲name =「command.Transaction [i] .Date」,並且它應該是name =「command.Transaction [@(i )]。Date「以便寫入i變量的值而不是字符'i'。 –

相關問題