简体   繁体   中英

MVC3 model instead model … can't see when posting to controller

I have an order model (shown below)

public class Order
    {
        //[Key]
        [ScaffoldColumn(false)]
        public int OrderId { get; set; }

        [DisplayName("Order Date")]
        public DateTime OrderDate { get; set; }

        public virtual ProductSelection ProductSelection { get; set; }

        public virtual ShippingDetails ShippingDetails { get; set; }

        public virtual BillingDetails BillingDetails { get; set; }

        public virtual CardDetails CardDetails { get; set; }

        public virtual AccountUser AccountUsers { get; set; }
    }

As you can see is made up of a set of other models for example ProductSelection (shown below).

public class ProductSelection
    {
        public int SimulatorId { get; set; }

        public string VersionNumber { get; set; }

        [DisplayName("Quantity")]
        public int Quantity { get; set; }

        [DisplayName("Total Price")]
        [ScaffoldColumn(false)]
        public decimal TotalPrice { get; set; }
    }

The issue I am having is when I post to the Controller which has a parameter of Order, I am unable to obtain any of the values from the sub-models (for example Order.ProductSelection.SimulatorId.)

Any ideas why this isn't working as I having to currently use FormCollection which isn't ideal and better messy.

Looking forward to replies

Steve

1) Silly question but just to make sure....Do you preserve values of your sub model on the view(In the form as hidden or any other input type,make sure name of your hidden are same as your properties name in the model) or in the query string. Before giving you fully loaded model, model binder looks at different places to load your model like your form collection,rout data and query string If you are not preserving them in any of these places then model binder has no way to find those values and give you loaded values on controller action.
Basics.. http://dotnetslackers.com/articles/aspnet/Understanding-ASP-NET-MVC-Model-Binding.aspx

2)Your example model seems fine but make sure all properties of your sub model have public access modifier and they must have set in their property declaration. --->I had same issue before because I had private access modifier for set on those properties and I wasted whole day to figure that out.

3)If nothing works(hope that's not the case) then at last you can write your own model binder. Here is the good post if you decide to head in that direction http://buildstarted.com/2010/09/12/custom-model-binders-in-mvc-3-with-imodelbinder/

This is my first post (under my account) and it feels really good to participate..!!

You should apply ForeignAttribute on the ProductSelection property which points the primary key of the ProductSelection class:

[ForeignKey("SimulatorId")]
public virtual ProductSelection ProductSelection { get; set; }

Hope that helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM