主从表结构的数据,如何通过模型绑定送回控制器


需求:在主从表结构中,从表是集合类型,数据在View中通过JS生成,要通过数据绑定传回控制器,在控制器中完成数据存储。

1) View Mode

    public class NewOrderView

    {

        public SaleMain Main { get; set; }

        public List<SaleDetail> Details {get; set; }

}

2) View中的引用

View中的集合要实现正确的绑定,要注意两点:

A. 显示在Table中,不能用DisplayFor, 只能用EdiorFor或TextBoxFor,否则不能绑定。

B. 要用带下标的方式表现LIST或Collection,绑定才能正确

m =>m.Details[i].CommodityID

如下面代码中的红色部分,是关键。

@model Store.ViewModels.NewOrderView

<table>

   <tr>

        <th>

            @Html.DisplayNameFor(model =>model.Details.First().CommodityID)

        </th>

        <th>

            @Html.DisplayNameFor(model =>model.Details.First().Quantity)

        </th>

        <th>

            @Html.DisplayNameFor(model =>model.Details.First().PriceUnit)

        </th>

        <th>

            @Html.DisplayNameFor(model =>model.Details.First().Subtotal)

       </th>

   </tr>

 

   @for (int i = 0; i < Model.Details.Count; i++)

   {

            <tr>

            <td>

               @Html.TextBoxFor(m => m.Details[i].CommodityID, null, new { @readonly = true, style= "width: 120px; background-color:#D7EBFF;" })

            </td>

            <td>

                 @Html.TextBoxFor(m =>m.Details[i].Quantity, null, new { @readonly = true, style = "width: 50px;background-color:#D7EBFF;" })

            </td>

            <td>

                @Html.TextBoxFor(m => m.Details[i].PriceUnit,null, new { @readonly = true, style = "width: 80px;background-color:#D7EBFF;" })

            </td>

            <td>

                @Html.TextBoxFor(m =>m.Details[i].Subtotal, null, new { @readonly = true, style = "width: 80px;background-color:#D7EBFF;" })

            </td>

        </tr>

   }

 

</table>

3) 控制器中用法

 

       [HttpPost]

        public ActionResult Create(NewOrderView NewOrder, string Add, string Create)

        {

             ….

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值