English 中文(简体)
MVC3模式,相反,模式......在被派到控制人时可能看不出。
原标题:MVC3 model instead model ... can t see when posting to controller

我有秩序模式(见下文)。

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; }
    }

我的问题是,当我向有命令参数的主计长担任职务时,我无法从分投票中获得任何价值(例如,命令。 SimulatorId.

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

期待答复

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/

这是我的第一个职位(在我的发言下),它感到真正良好的参与!

您应在<代码>ProductSelection/code>上适用标明<代码>主要关键人物的财产。 产品产品选择类别:

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

希望会有所帮助。





相关问题
WebForms and ASP.NET MVC co-existence

I am trying to make a WebForms project and ASP.NET MVC per this question. One of the things I ve done to make that happen is that I added a namespaces node to the WebForms web.config: <pages ...

Post back complex object from client side

I m using ASP.NET MVC and Entity Framework. I m going to pass a complex entity to the client side and allow the user to modify it, and post it back to the controller. But I don t know how to do that ...

Create an incremental placeholder in NHaml

What I want to reach is a way to add a script and style placeholder in my master. They will include my initial site.css and jquery.js files. Each haml page or partial can then add their own required ...

asp.net mvc automapper parsing

let s say we have something like this public class Person { public string Name {get; set;} public Country Country {get; set;} } public class PersonViewModel { public Person Person {get; ...

structureMap mocks stub help

I have an BLL that does validation on user input then inserts a parent(PorEO) and then inserts children(PorBoxEO). So there are two calls to the same InsertJCDC. One like this=>InsertJCDC(fakePor)...

ASP.NET MVC: How should it work with subversion?

So, I have an asp.net mvc app that is being worked on by multiple developers in differing capacities. This is our first time working on a mvc app and my first time working with .NET. Our app does not ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

热门标签