Asp.net mvc MVC2控制器在提交时未从视图接收所有数据

Asp.net mvc MVC2控制器在提交时未从视图接收所有数据,asp.net-mvc,asp.net-mvc-2,controller,viewmodel,asp.net-mvc-viewmodel,Asp.net Mvc,Asp.net Mvc 2,Controller,Viewmodel,Asp.net Mvc Viewmodel,我有一个应用程序,它调用web服务,根据用户输入的条件显示导入记录 每个“进口”可以有0到多个采购订单、0到多个容器、0到多个产品、0到多个发票 视图(显示查询)工作正常,但当我点击提交按钮时,控制器没有通过viewmodel获得“导入”信息。我可以通过“formCollection”访问这些控件 我错过了什么 这是我的密码 模型 namespace LemansCorpIntranet.Models { public class ImportWebViewModel {

我有一个应用程序,它调用web服务,根据用户输入的条件显示导入记录

每个“进口”可以有0到多个采购订单、0到多个容器、0到多个产品、0到多个发票

视图(显示查询)工作正常,但当我点击提交按钮时,控制器没有通过viewmodel获得“导入”信息。我可以通过“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; }
    }  
}
我还没有发布完整的视图/控制器

它用于渲染视图

当我提交视图/表单时,控制器只能访问viewmodel主要部分中的项目(“列表”中没有任何内容)

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


我想知道为什么viewmodel中有空值。

我想您缺少的是一些
@HiddenFor
子句,用于模型中没有的值。如果我理解正确的话,你会得到其中一些,但不是全部。因此,对于那些您没有得到的,请为它们定义
@HiddenFor
,然后看看会发生什么

更新

这就是堆栈溢出对嵌套视图模型的说明:

更新2

看看这里,似乎与您的问题非常相似:


希望这对您有所帮助。

您的问题在于,您没有以MVC model binder能够理解的方式命名字段,因为它们是嵌套类的一部分。对于每一行,都有一个同名的字段。那不行

这是您不应该使用非类型安全版本的helper类的原因之一,因为类型安全版本将自动生成正确的名称。您还应该利用EditorTemplates,因为它们对集合执行正确的操作,并自动迭代集合

阅读

如果必须按当前方式执行,则需要在名称中包含数组语法。使用for语句而不是foreach语句,并使用计数器计算行号。这样做:

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

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

    ....
<% } %>

....
Model.import\u rows[行].更新\u开关)%>
....

请阅读此处的更多信息:

我试图在重定向操作中传递viewmodel

我发现这样做不起作用


谢谢大家的帮助。

代码太多了。你能不能把范围缩小到文章中没有提到的项目?我在上面进行了更新,以精简代码……你可能需要为每个“嵌套”类的每个属性都添加一个隐藏元素。问题不在于数据进入模型,从而呈现视图。渲染视图工作正常,当视图“发布”时,视图模型的某些部分无法到达控制器。嵌套类是问题所在。。。
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);
        }
<% for(int row = 0; row < Model.import_rows.Count; row++) { %>
    ....

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

    ....
<% } %>