0

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

每个“导入”可以有 0 到多个 PurchaseOrder、0 到多个 Containers、0 到多个 Products、0 到多个 Invoices。

视图(显示查询)工作正常,当我单击提交按钮时,控制器没有通过视图模型获取“导入”信息。我可以通过“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);
        }

我还没有发布视图/控制器的完整性......

它可以渲染视图....

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

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

我想知道为什么视图模型中有空值。

4

3 回答 3

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

于 2012-09-24T19:43:38.170 回答
0

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

更新

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

asp.net MVC 中的嵌套 ViewModel 类

更新 2

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

ASP.net MVC2。填充模型未返回控制器

希望这可以帮助

于 2012-09-21T22:57:27.020 回答
0

我试图在 RedirectToAction 中传递视图模型..

发现这行不通。

感谢大家的帮助。

于 2012-09-25T21:43:35.537 回答