在MVC帖子视图中将多个项目添加到List

时间:2016-12-01 14:08:08

标签: c# asp.net-mvc model

我的主要观点是在视图中同时添加多个项目行。 我尝试了很多不同的方法,我想要一些帮助。

我很感激有关如何解决此问题的建议。我很感激一切。代码评论,反馈,链接,指南,信息,帮助,消息。与此问题有关的任何事情。

The Models

这是我的最终代码。

public class Order
{
    public int OrderID { get; set; }

    public int EmployeeID { get; set; }
    public virtual Employee Employee { get; set; }

    public virtual ICollection<OrderRowDetails> OrderRowDetails { get; set; }
}

员工模型:

public class Employee
{
    public int EmployeeID { get; set; }
    public string EmployeeName { get; set; }
}

OrderRowDetails:

public class OrderRowDetails
{
    public int OrderRowDetailsID { get; set; }
    public int OrderID { get; set; }
    public int ProductID { get; set; }
    public virtual Product Product { get; set; }
    public int Quantity { get; set; }
}

public class Product
{
    public int ProductID { get; set; }
    public string ProductName { get; set; }
    public int ProductTypeID { get; set; }
    public virtual ProductType ProductType { get; set; }
}

public class ProductType
{
    public int ProductTypeID { get; set; }
    public string ProductTypeName { get; set; }
}

现在我使用View Model:

public class OrderViewModel
{
    public int OrderID { get; set; }

    public int EmployeeID { get; set; }

    public IEnumerable<OrderRowDetails> OrderRowDetails { get; set; }

    public IEnumerable<Employee> Employees { get; set; }

    public int OrderRowDetailsID { get; set; }
    public int ProductID1 { get; set; }
    public IEnumerable<Product> Products { get; set; }
    public int Quantity1 { get; set; }

    public int ProductID2 { get; set; }
    public int Quantity2 { get; set; }
    public int ProductID3 { get; set; }
    public int Quantity3 { get; set; }

    public OrderViewModel()
    {
       OrderRowDetails = new List<OrderRowDetails>();

    }

这是我的创建视图:

 @model TheProject1.ViewModel.OrderViewModel  
<table>
    <tr>
        <th>Employee ID: (1)</th>
        <td>@Html.TextBoxFor(m => m.EmployeeID)</td>
    </tr>      
<tr>
<th>Product ID: (1-2)</th>
<td>@Html.EditorFor(m => m.ProductID1)</td>
</tr>


    <tr>
        <th>Quantity</th>
        <td>@Html.EditorFor(m => m.Quantity1)</td>
    </tr>
    <tr>
        <th>Product ID: (1-2)</th>
        <td>@Html.EditorFor(m => m.ProductID2)</td>
    </tr>
    <tr>
        <th>Quantity</th>
        <td>@Html.EditorFor(m => m.Quantity2)</td>
    </tr>
    <tr>
        <th>Product ID: (1-2)</th>
        <td>@Html.EditorFor(m => m.ProductID3)</td>
    </tr>
    <tr>
        <th>Quantity</th>
        <td>@Html.EditorFor(m => m.Quantity3)</td>
    </tr>
    <tr>
        <td></td>
        <td><input type="submit"  value="Save" /></td>
    </tr>
</table>


    Controller:
    public ActionResult Create()
    {           
        return View();
    }

    public ActionResult Create(OrderViewModel orderView, Order order,               OrderRowDetails orderRowDetails)
    {

        var theorder = new Order()
        {
            EmployeeID = orderView.EmployeeID,
            OrderID = orderView.OrderID,
            OrderRowDetails = new List<OrderRowDetails>()
            {

                new OrderRowDetails()
                {
                    OrderID = orderView.OrderID,
                    ProductID = orderView.ProductID1,
                    Quantity = orderView.Quantity1
                },
                                    new OrderRowDetails()
                {
                    OrderID = orderView.OrderID,
                    ProductID = orderView.ProductID2,
                    Quantity = orderView.Quantity2
                },
                                                        new OrderRowDetails()
                {
                    OrderID = orderView.OrderID,
                    ProductID = orderView.ProductID3,
                    Quantity = orderView.Quantity3
                },
            }
        };


        db.Orders.Add(theorder);
        db.SaveChanges();

        // OrderID == OrderID
        return RedirectToAction("Index", "Order");

我输了很多,所以我的想法是创建5行,因此我不能有空值。我的想法是创建一个替代产品ID = 1是“没有价值” 在添加到数据库之前,我会从OrderRowDetails表中删除ProductID 1的行。

    <tr>
        <th>Product ID: (1-2)</th>
        <td>@Html.EditorFor(m => m.ProductID)</td>
    </tr>
    <tr>
        <th>Quantity</th>
        <td>@Html.EditorFor(m => m.Quantity)</td>
    </tr>
    <tr>
        <th>Product ID: (1-2)</th>
        <td>@Html.EditorFor(m => m.ProductID)</td>
    </tr>
    <tr>
        <th>Quantity</th>
        <td>@Html.EditorFor(m => m.Quantity)</td>
    </tr>

如果我这样做,我会从第一个OrderRow获得2次值。

我的目标是在列表中添加多个项目(OrderRowDetails),并将关系工具添加到Order(使用OrderID)。

如果有人能给我任何信息或教我如何解决这个问题,我真的很想。

0 个答案:

没有答案