开发者

how to implement Create action on Order and Order Details on single Create View?

anyone came up with simple solutions?

question: Given the two model classes with a relationship of one-to-many, how can i design a single create for these..

how can i add order details collection to a order entity that is not persisted yet in the database? and what viewmodels will i use? given that i have Order page(master) that is completed form then i must have a partial view that collects order details and after that when i clicked 'save', it will create the 'Order' plus its collection of 'order details'. thanks

a simple solution of codes will be helpful. thanks

Here's my models:

public class Order
    {
        [Key]
        public int OrderID { get; set; }

        [DisplayFormat(NullDisplayText = "0.00")]
        public decimal TotalAmount { get { return OrderDetails.Sum(od => od.Amount); } }

        public decimal Cash { get; set; }

        [DisplayFormat(NullDisplayText = "0.00")]
        public decimal TotalBV { get { return OrderDetails.Sum(od => od.BV); } }

        public int MemberID { get; set; }开发者_Go百科

        public virtual Member Member { get; set; }

        public virtual ICollection<OrderDetail> OrderDetails { get; set; }
    }

public class OrderDetail
{
    [Key]
    public int OrderDetailID { get; set; }
    public int OrderID { get; set; }

    public decimal Amount { get { return Product.Price * Quantity; } }

    public decimal BV { get { return Product.BV * Quantity; } }

    public int Quantity { get; set; }
    public virtual Product Product { get; set; }
}

Thanks.


The short answer is really "it depends" :) The question you need to ask first (and it has nothing to do with MVC) is the following: when do you want to commit user entries? I can think of several options:

  • You have a "create order" page. User fills the header fields (looks like Member is the only one), hits save, you save order in the database, get the order ID, make the header fields read-only and the user starts entering lines.
  • The form has both header fields and detail fields on the entry form (personally, I find it confusing - but it's a matter of taste). You get all the data (probably in form collection) and build Order and Order details objects out of that (either in the controller, or through custom provider)

Additionally, you need to answer the same question about order details. Do you want to submit every row, or allow the user to create multiple rows (through some "Add Row" button and JavaScript code behind it) and then submit a set of rows.

Once you decide which way you want to go - it will be easier to design the form. If you run into problems - come back; I am sure you will get plenty of help!

God luck


I don't know how clever MVC's binding is since I use my own binding so I don't know whether it can do the binding to models for you.

What you can do is to number all the inputs for each line. So Product1, 'Amount1`, etc. For each new line (say there is an 'Add Detail' button) you create a new set of inputs with the next available number.

When you parse the Request.Form you simply ask for each line until you cannot find one --- then you know you are done.

HTH

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜