2015-11-07 156 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'。 –

相关问题