【问题标题】:How to code a viewModel with nested classes如何使用嵌套类编写 viewModel
【发布时间】:2012-09-24 18:19:46
【问题描述】:

我的网页/视图将有 1 到多个导入记录。 在每条导入记录内将是 0 到以下多个:

  • 采购订单
  • 集装箱
  • 产品
  • 发票

我的视图模型编码如下:

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;
    }

    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; }
    }  
}

以上可以吗?类嵌套是否正确?

我可以让视图正确显示来自服务器的数据,但是当我“发布”表单时,嵌套类中的数据为空。

我相信这是因为输入控件没有按需要构建。

我的看法类似如下:

<table>
<% if (Model.import_rows != null)
   { %>
<%    for (int row = 0; row < Model.import_rows.Count; row++)
      { %>
        <tr><td>
                <%=Html.HiddenFor(x=>x.import_rows[row].id) %>
                <%=Html.TextBoxFor(x => x.import_rows[row].id)%>
            </td>
        </tr>
<%    } %>
<% } %>
</table>

任何建议将不胜感激......

更新:我已更改视图以包含“hiddenFor”项目。

“import_rows”在控制器中不再为空,但计数为“0”。 我一定还是错过了什么.....

提前致谢。

【问题讨论】:

  • 您需要展平您的模型,或者为每个“嵌套”类中的每个属性创建一个隐藏字段,或者当您返回 post 方法时从数据库中重新获取数据。

标签: asp.net-mvc controller viewmodel asp.net-mvc-viewmodel


【解决方案1】:

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

发现这样不行。

感谢大家的帮助。

【讨论】:

    猜你喜欢
    • 2023-03-05
    • 2017-08-29
    • 1970-01-01
    • 2019-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多