MVC多表一个模型

时间:2013-10-12 13:47:53

标签: asp.net-mvc asp.net-mvc-4 model-view-controller viewbag object-oriented-database

我很难理解来自aspx世界的MVC。

我有一个名为CustomerGarment的模型。这有OrderCustomer以及一些服装。

public class CustomerGarment
{
    public int CustomerGarmentId { get; set; }

    public virtual Customer Customer { get; set; }

    public virtual Order Order { get; set; }

    public virtual GarmentJacket GarmentJacket { get; set; }
    public virtual GarmentShirt GarmentShirt { get; set; }
}

我有一个获取和发布的方法。页面加载时,它会创建一个新的CustomerGarment实例并查询数据库以填充Customer和Order变量。然后我使用viewbag在屏幕上显示GarmentJacketGarmentShirt s的列表

然后页面查看并使用视图我可以完美地访问模型。使用viewbag内容减少负载,我可以使用我传递的模型访问所有Customer和Order变量。

我接下来遇到的问题是当我使用HttpPost时。模型不会传递给我传递给它的信息。

    public ActionResult AddGarments(int orderId, int customerId)
    {
        CustomerGarment cg = new CustomerGarment();
        cg.Order = (from a in db.Orders where a.OrderId == orderId select a).FirstOrDefault();
        cg.Customer = (from a in db.Customers where a.CustomerId == customerId select a).FirstOrDefault();

        var jackets = from a in db.GarmentJackets orderby a.Type, a.SleeveLengthInches, a.ChestSizeInches select a;
        var shirts= from a in db.GarmentKilts orderby a.PrimarySize, a.DropLength select a;
        ViewBag.GarmentJacket = new SelectList(jackets, "GarmentJacketId", "GarmentJacketId");
        ViewBag.GarmentShirt = new SelectList(shirts, "GarmentShirtId", "GarmentShirtId");

        return View(cg);
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult AddGarments(CustomerGarment cg)
    {
        // Here, I do not have the customer info for example
        db.CustomerGarments.Add(cg);
        db.SaveChanges();
        return RedirectToAction("Index");

        return View(cg);
    }

这是我的观点

@Html.HiddenFor(model => model.Order.OrderId)
    @Html.HiddenFor(model => model.Order.CustomerId)

    <div class="display-field">
        @Html.DisplayFor(model => model.Customer.Name)
    </div>


    <div class="editor-label">
        @Html.LabelFor(model => model.GarmentJacket, "Jacket")
    </div>
    <div class="editor-field">
        @Html.DropDownListFor(m => m.GarmentJacket, (SelectList)ViewBag.GarmentJacket, new {style="width:312px;height:30px;margin-top:2px;margin-bottom:5px"})
    </div>

修改

我的服装夹克模特

public class GarmentJacket : Garment
{
    public int GarmentJacketId { get; set; }

    [Required]
    public string Type { get; set; }

    [Required]
    [Display(Name = "Chest Size")]
    public int ChestSizeInches { get; set; }

    [Required]
    [Display(Name = "Sleeve Length")]
    public int SleeveLengthInches { get; set; }
 }

public class Garment
{
    [DataType(DataType.Date)]
    public DateTime? DateRetired { get; set; }

    [Required]
    public string Barcode { get; set; }

    [Required]
    public bool Adults { get; set; }
}

2 个答案:

答案 0 :(得分:2)

在您的CustomerGarment类中,您应该:

public class CustomerGarment
{
    public int CustomerGarmentId { get; set; }
    public int CustomerId { get; set; }
    public int OrderId { get; set; }
    public int GarmentJacketId { get; set; }
    public int GarmentShirtId { get; set; }

    public virtual Customer Customer { get; set; }
    public virtual Order Order { get; set; }
    public virtual GarmentJacket GarmentJacket { get; set; }
    public virtual GarmentShirt GarmentShirt { get; set; }
}

然后,在您的视图中,您的DropDownList将如下所示:

@Html.DropDownListFor(m => m.GarmentJacketId, (SelectList)ViewBag.GarmentJacket, new {style="width:312px;height:30px;margin-top:2px;margin-bottom:5px"})

你的DropDownList只发布一个值,即GarmentJacketId。您无法将该Id绑定到整个GarmentJacket类。

顺便说一句,您还需要用以下内容替换隐藏的输入:

@Html.HiddenFor(model => model.OrderId)
@Html.HiddenFor(model => model.CustomerId)

答案 1 :(得分:1)

我想我知道你的问题。正如您在上面评论中所建议的那样,您需要在视图中发布您想要保留的所有内容。这是webforms和MVC之间的差异之一,webforms的viewstate可能包含您未明确添加到视图并回发的信息,给人以状态的印象。在MVC中,您必须将其添加到视图中。

另一方面,您不需要传递比您需要的更多信息。您将customerId作为隐藏字段传递给客栈。在post方法中,您使用Id从db获取客户,然后将订单添加到客户。

我对您的设计有一些疑问,但鉴于客户持有订单集合,您可以执行以下操作:

 [HttpPost]
[ValidateAntiForgeryToken]
public ActionResult AddGarments(CustomerGarment cg)
{

    // Get the customer from the database
    var customer = db.Customers.Find(c=>c.id==cb.Customer.Id)
    var order = new Order();
    //Create your order here using information from CustomerGarment model
    //If the model already holds a valid Order object then just add it.
    //i.e. you could get a Garment object from the DB using the GarmentId from
    //the ViewModel if you really need more than just the Id to create the order

    customer.Orders.Add(order);
    db.SaveChanges();
    return RedirectToAction("Index");

}