MVC 4局部视图中的“添加/更新"列表 [英] MVC 4 Add/Update list in a partial view

查看:66
本文介绍了MVC 4局部视图中的“添加/更新"列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的第一个程序,我不太确定如何完成我的代码.我正在尝试创建一个新的交易,对于一个供应商,这里将有多种库存类型.我不知道如何使用部分视图将清单类型添加到列表中.任何有关如何构造代码的指导将不胜感激.这是我的代码:

This is my first program and I am quite not sure on how to complete my codes. I am trying to create a new transaction, where for a supplier there will be multiple inventory types. I am lost on how to add the inventory types to a list using Partial View. Any guidance on how to structure the code will be greatly appreciated. Here are my codes:

ViewModels

public class InventoryTransactionParent
    {
        [Key]
        public int InventoryTransactionID { get; set; }

        [ForeignKey("InventoryTransactionType")]
        [Display(Name = "Transaction Type")]
        public int InventoryTransactionTypeID { get; set; }
        public virtual InventoryTransactionType InventoryTransactionType { get; set; }

        [Display(Name = "Supplier")]
        [ForeignKey("Supplier")]
        public int? SupplierID { get; set; }
        public virtual Supplier Supplier { get; set; }

        [Display(Name = "Transaction Date (From previous month only)")]
        [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
        public DateTime InventoryTransactionDate { get; set; }

        [Display(Name = "Receipt/Invoice No.")]
        public string InventoryTransactionReceipt { get; set; }

        [Display(Name = "Transaction By")]
        public string InventoryTransactionBy { get; set; }

        [Display(Name = "Created On")]
        public DateTime InventoryTransactionCreatedDateTime { get; set; }

        [Display(Name = "Created By")]
        public string InventoryTransactionCreatedBy { get; set; }

        public bool InventoryTransactionCancelled { get; set; }
        public int? InventoryTransactionCancelledSourceID { get; set; }

        public List<InventoryTypeChild> InventoryTypeChilds { get; set; }
    }

public class InventoryTypeChild
    {
        [ForeignKey("InventoryType")]
        [Display(Name = "Inventory Type")]
        public int InventoryTypeID { get; set; }
        public virtual InventoryType InventoryType { get; set; }

        [Display(Name = "Quantity")]
        public decimal InventoryTransactionQuantity { get; set; }

        [Display(Name = "Price per Item")]
        public decimal InventoryTransactionPrice { get; set; }

        [Display(Name = "Remarks (1000 characters)")]
        [DataType(DataType.MultilineText)]
        public string InventoryTransactionRemarks { get; set; }
    }

查看:

@model Inventory.ViewModels.InventoryTransactionParent
    @using (Html.BeginForm()) {
    @Html.ValidationSummary(true)



  <fieldset>
        <legend>In Transaction</legend>


        <div class="editor-label">
            @Html.LabelFor(model => model.SupplierID, "Supplier")
        </div>
        <div class="editor-field">
            @Html.DropDownList("SupplierID", String.Empty)
            @Html.ValidationMessageFor(model => model.SupplierID)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.InventoryTransactionDate)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.InventoryTransactionDate, "TransactionDate")
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.InventoryTransactionReceipt)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.InventoryTransactionReceipt)
            @Html.ValidationMessageFor(model => model.InventoryTransactionReceipt)
        </div>
        <div id="inventorytypes">
            @using (Html.BeginForm()) {
                <table>
                    <tr>
                        <td>@Html.LabelFor(model => Model.InventoryTypeChilds[0].InventoryTypeID)</td>
                        <td>@Html.LabelFor(model => Model.InventoryTypeChilds[0].InventoryTransactionPrice)</td>
                        <td>@Html.LabelFor(model => Model.InventoryTypeChilds[0].InventoryTransactionQuantity)</td>
                        <td>@Html.LabelFor(model => Model.InventoryTypeChilds[0].InventoryTransactionRemarks)</td>
                        <td></td>
                    </tr>
                @{
                    if (Model.InventoryTypeChilds != null)
                    {
                        for (int i = 0; i < Model.InventoryTypeChilds.Count(); i++)
                        {
                            <tr>
                                <td>
                                    @Html.DropDownList("InventoryTypeID", String.Empty)
                                    @Html.ValidationMessageFor(model => model.InventoryTypeChilds[i].InventoryTypeID)
                                </td>
                                <td>
                                    @Html.EditorFor(model => Model.InventoryTypeChilds[i].InventoryTransactionPrice)
                                    @Html.ValidationMessageFor(model => Model.InventoryTypeChilds[i].InventoryTransactionPrice)
                                </td>
                                <td>
                                    @Html.EditorFor(model => Model.InventoryTypeChilds[i].InventoryTransactionQuantity)
                                    @Html.ValidationMessageFor(model => Model.InventoryTypeChilds[i].InventoryTransactionQuantity)
                                </td>
                                <td>
                                    @Html.EditorFor(model => Model.InventoryTypeChilds[i].InventoryTransactionRemarks)
                                    @Html.ValidationMessageFor(model => Model.InventoryTypeChilds[i].InventoryTransactionRemarks)
                                </td>
                                <td>
                                    <input type="submit" value="Add" />
                                </td>
                            </tr>
                        }
                    }
                }
                </table>
            }
        </div>
        <p>
            <input type="submit" value="In" />
        </p>
    </fieldset>
}

控制器(仍然很粗糙):

public ActionResult InMultipleTransaction()
        {
            ViewBag.InventoryTypeID = new SelectList(db.InventoryTypes, "InventoryTypeID", "InventoryTypeName");
            ViewBag.SupplierID = new SelectList(db.Suppliers, "SupplierID", "SupplierName");
            InventoryTransactionParent itp = new InventoryTransactionParent();
            itp.InventoryTypeChilds = new List<InventoryTypeChild>();
            itp.InventoryTypeChilds.Add(new InventoryTypeChild()
            {

            });

            return View(itp);
        }

推荐答案

您无需在one-to-many关系中使用Partial Views.如果同时创建父对象和子对象,并且大多数情况下发生在one-to-one关系中,则Partial Views很有用(但即使如此,大多数人还是使用ViewModels).但是在您的情况下,必须先创建父对象,然后再添加许多子对象.

You don't need to use Partial Views in one-to-many relationship. Partial Views are useful if parent and child objects are being created at the same time, mostly happens in one-to-one relationship (But even then, most people use ViewModels). But in your case, you have to have parent object created first then you add many child objects.

此Plural Sight视频将是一个不错的开始.有免费试用版

这篇关于MVC 4局部视图中的“添加/更新"列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆