2

我对来自 aspx 世界的 MVC 的理解有困难。

我有一个名为CustomerGarment. 这有一个Order和一个Customer以及一些服装。

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 在屏幕上显示GarmentJackets 和GarmentShirts的列表

然后页面查看并使用该视图我可以完美地访问模型。下拉菜单加载视图包内容,我可以使用我传递的模型访问所有 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; }
}
4

2 回答 2

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)
于 2013-10-12T14:38:03.950 回答
1

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

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

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

 [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");

}
于 2013-10-12T14:55:05.520 回答