0

我有一个应用程序调用Web服务来显示基于用户输入的标准的导入记录。MVC2控制器没有收到提交视图中的所有数据

每个“导入”可以有0到多个PurchaseOrder,0到很多容器,0到很多产品,0到很多发票。

查看(显示查询)工作正常,bur当我点击一个提交按钮控制器没有通过视图模型获取“导入”信息。我可以通过“formCollection”访问这些控件。

我在想什么?

这里是我的代码...

型号...

namespace LemansCorpIntranet.Models 
{  
    public class ImportWebViewModel 
    { 
     public string button { get; set; } 
     public string status_filter { get; set; } 
     public string import_id_filter { get; set; } 
     public DateTime date_filter { get; set; } 
     public string vendor_filter { get; set; } 
     public string port_filter { get; set; } 
     public string whse_filter { get; set; } 
     public Boolean not_released_filter { get; set; } 
     public int release_gap { get; set; } 
     public List<import_row> import_rows; /// this is the piece that is not coming 
               // thru on the POST 
    } 

    public class import_row 
    { 
     public string update_switch { get; set; } 
     public string id { get; set; } 
     public IEnumerable<SelectListItem> ship_via { get; set; } 
     public string broker_number { get; set; } 
     public string voyage { get; set; } 
     public string vessel { get; set; } 
     public decimal shipment_value { get; set; } 
     public int cartons { get; set; } 
     public decimal weight { get; set; } 
     public decimal volume { get; set; } 
     public string clearance_port { get; set; } 
     public string warehouses_in_shipment { get; set; } 
     public string payment_type { get; set; } 
     public string insurance { get; set; } 
     public DateTime ship_date { get; set; } 
     public DateTime close_date { get; set; } 
     public DateTime customs_date { get; set; } 
     public string customs_entry { get; set; } 
     public DateTime pl_2_whse_date { get; set; } 
     public DateTime estimated_arrival_date { get; set; } 
     public DateTime wire_transfer_request_done_date { get; set; } 
     public DateTime approved_broker_bill_date { get; set; } 
     public DateTime product_released_date { get; set; } 
     public List<Invoice> Invoices; 
     public List<PurchaseOrder> PurchasOrders; 
     public List<Product> Products; 
     public List<Container> Containers; 
    } 

    public class Invoice 
    { 
     public string invoice_number { get; set; } 
    } 

    public class PurchaseOrder 
    { 
     public string id { get; set; } 
     public string whse { get; set; } 
     public string vendor_code { get; set; } 
     public string vendor_name { get; set; } 
    } 

    public class Product 
    { 
     public int line_number { get; set; } 
     public string description { get; set; } 
    } 

    public class Container 
    { 
     public int line_number { get; set; } 
     public int size { get; set; } 
     public string id { get; set; } 
     public string seal { get; set; } 
     public DateTime received_date { get; set; } 
     public int cartons { get; set; } 
    } 
} 

这里是我的图(局部)

<% using (Html.BeginForm("ImportLog", "ImportWeb")) %> 
    <% { %> 
      <table style="position:fixed"> 
. 
. 
. 
<% if (Model.import_rows != null) %> 
<% { %> 
<% var row_number = 0; %> 
    <table id="import_web_detail"> 
<% foreach (var row in Model.import_rows) %> 
<% { %> 
<%  if (row.id != "   ")%> 
<%  { %> 
<%  row_number++; %> 
     <tr style="vertical-align:top"> 
      <td style="width:.5em" /> 
      <td align="center"> 
      <table> 
       <tr> 
       <td colspan="4"></td> 
       <td><%=Html.CheckBox("update_switch", row.update_switch)%></td> 
       <td colspan="4"></td> 
       </tr> 
      </table> 
      </td> 
      <td /> 
      <td> 
      <table> 
       <tr> 
       <td><%=Html.TextBox("import_id", row.id)%></td> 
       </tr> 
      </table> 
      </td> 

这是我的...位指示

namespace LemansCorpIntranet.Controllers 
{ 
    public class ImportWebController : Controller 
    { 
     eptest_importweb_svc.Service importweb_svc = new eptest_importweb_svc.Service(); 

     // 
     // Import Web Request 

     public ActionResult ImportLog(ImportWebViewModel import_request) 
     {// import_request.import_rows is null. should be loaded with view detail 
      switch (import_request.button) 
      { 
       case "Next": 
       case "Previous": 
        return RedirectToAction("ImportPage", import_request); 
       case "Update": 
        return RedirectToAction("ImportUpdate"); 
       case "Importlog": 
        return RedirectToAction("ImportReport"); 
       default: 
        break; 
      } 
      // ---------------------------------- 
      // fall thru for initial page display 
      // ---------------------------------- 


      //  load "date filter" with default. 300 days in the past. 
      //  ------------------------------------------------------ 
      import_request.date_filter = new DateTime(DateTime.Now.Year - 1, 
            DateTime.Now.Month, DateTime.Now.Day).AddDays(65); 

      return View("ImportLog", import_request); 
     } 

我还没有发布完整的t他查看/控制器....

它的工作原理渲染视图....

当我提交视图/形成控制器只能访问的视图模型的主要部分(项目无在“列表”通过)。

我可以通过“formcollection”访问这些其他控件。

我想知道为什么viewmodel中有null。

+0

这是很多代码。你能缩小到哪些项目没有通过帖子? –

+0

我更新了上面的代码以削减代码.... – hyperstaff

+0

您可能需要为每个“嵌套”类的每个属性隐藏一个元素。 –

回答

0

我正在试图通过视图模型在RedirectToAction ..

发现这并不是这样的。

感谢大家的帮助。

0

我认为你缺少的是几个@HiddenFor子句你没有在模型中得到的值。如果我正确地理解了你,你会得到一些但不是全部。所以那些你没有得到,为他们定义@HiddenFor,看看会发生什么。

更新

这是堆栈溢出说关于嵌套视图模型:

Nested ViewModel Classes in asp.net MVC

更新2

看进到这里,似乎非常相似,你的问题:

ASP.net MVC2. Populated model is not getting back to the controller

希望这有助于

+0

这个问题与数据不在模型中并因此渲染视图有关。渲染视图可以正常工作,当视图“张贴”时,viewModel的某些部分没有进入控制器。嵌套类是问题... – hyperstaff

1

你的问题是,你是不是在某种程度上MVC模型绑定可以理解,他们是一个嵌套类的部分命名你的领域。对于每一行,只需要一个具有相同名称的字段。这是行不通的。

这是您不应使用助手类的非类型安全版本的原因之一,因为类型安全版本会自动生成正确的名称。您还应该利用EditorTemplates,因为他们为集合做了正确的事情,并自动迭代集合。

阅读http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-1-introduction.html

如果你必须这样做你正在做的方式,那么你需要在名称中包含数组语法。使用for语句而不是foreach,并使用计数器来计算行号。做这样的事情:

<% for(int row = 0; row < Model.import_rows.Count; row++) { %> 
    .... 

    <td><%=Html.CheckBoxFor(x => Model.import_rows[row].update_switch)%></td> 

    .... 
<% } %> 

更多在这里阅读:http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx

相关问题