MVCWizard.Wizard - 使用复杂模型 [英] MVCWizard.Wizard - Using Complex Models

查看:99
本文介绍了MVCWizard.Wizard - 使用复杂模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是想用MVCWizard.Wizard从作为的NuGet多页向导用户界面的一部分。

我有一些家长模型类,以子类作为小节例如

 公共类MainModel
{
公共虚拟INT MainModelId {搞定;组; }
公共虚拟PersonalDetails CustomerPersonalDetails {搞定;组; }
公共虚拟AddressDetails CustomerAddressDetails {搞定;组; }
公共虚拟FinanceDetails CustomerFinanceDetails {搞定;组; }
}

经过反复试验,我发现,使用MVCWizard.Wizard,我必须每款网页创建一个WizardManager我想,以显示(这么一WizardManager,控制器,局部为CustomerPersonalDetails意见等。 ,一个用于CustomerAddressDetails等..)

..到目前为止好..

创建为每款控制器从WizardController T,其中T是模型继承。我想要做的是code控制器每个小节使用接受MainModel的条款内容,并从那里只是通过MainModel.Child每个partialview要求(的方式MVCWizard.Wizard似乎需要)

这样做,这样,这一切编译确定,但在使用中,导航来回向导页面(局部视图)的base.Wizard()之间的数据。WizardModel是越来越歼灭。

我发现,让导航回来时,它在Base.Wizard()。WizardModel保留数据来回,我必须明确地指定每个WizardManager ..结果

子类的类型

所以..(控制器)

 公共myController的:WizardManager< MainModel> ...
公共PartialViewResult SubsectionPage1(MainModel MDL)..

(视图)

  @model MainModel
的CustName:@ Html.TextBoxFor(M = GT; m.CustomerPersonalDetails.CustName)

..看起来像它的工作原理,但数据不保留。但是..搜索结果(控制器)

 公共myController的:WizardManager< PersonalDetails> ...
公共PartialViewResult SubsectionPage1(PersonalDetails细节)..

(视图)

  @model PersonalDetails
的CustName:@ Html.TextBoxFor(M = GT; m.CustName)

作品就好了...有什么我失踪了为什么MVCWizard.Wizard似乎无法处理复杂的车型占据着一个或多个子的类型?最后,我可以code,所以我通过这些小模型的WizardManager,然后坚持到数据存储之前穿过我的域模型结合起来,将它们映射,但我会作出解释感谢。

*编辑*

好吧,我周围挖了一点,初始模型绑定应该工作在最正常的情况下就好了。但使用MVCWizard.Wizard时,这个问题似乎是与MVCWizard.Wizard的EnableSimpleMerge功能。其页面导航过程中使用并似乎有与该合并函数如何使用反射来发现模型中的properies的问题。从我最初看,它似乎并不为coded到一个更复杂的模型结构中通过递归和发现子对象..

 私人无效合并(T向导,T型)
        {
            的foreach(在model.GetType()的PropertyInfo信息。的GetProperties())
            {
                反对OBJ2 = info.GetGetMethod()调用(模型,空)。
                如果(OBJ2!= NULL)
                {
                    。wizard.GetType()的getProperty(info.Name).GetSetMethod()调用(向导,新的对象[] {} OBJ2)。
                }
            }
            的foreach(字段信息INFO3在model.GetType()。GetFields())
            {
                对象obj3 = info3.GetValue(模型);
                如果(obj3!= NULL)
                {
                    。wizard.GetType()getfield命令(info3.Name).SetValue(向导,obj3);
                }
            }
        }


解决方案

(见上文评论:)

需要使用深入反思物业写的更详细的合并方法

Just trying to use MVCWizard.Wizard from NuGet as part of a multipage Wizard UI.

I have some "parent" model class, with "child" classes as subsections e.g.

public class MainModel
{
public virtual int MainModelId { get; set; }
public virtual PersonalDetails CustomerPersonalDetails { get; set; }
public virtual AddressDetails CustomerAddressDetails{ get; set; }
public virtual FinanceDetails CustomerFinanceDetails{ get; set; }
}

Through trial and error, I've found that to use MVCWizard.Wizard, I have to create a "WizardManager" per subsection of pages I'd like to display (so one WizardManager, controller, partial views etc. for CustomerPersonalDetails, one for CustomerAddressDetails and so on..)

..so far so good..

Creating the controller for each subsection inherits from WizardController T, where T is the model. What I'd like to do is code the controller each subsection uses to accept the MainModel in its entirity, and from there just pass MainModel.Child to each partialview as required (the way the MVCWizard.Wizard seems to require)

Doing it this way, it all compiles ok but in use, navigating back and forth between the wizard pages (partial views) the data in base.Wizard().WizardModel is getting wiped out.

I found, that to get it to retain data in the Base.Wizard().WizardModel when navigating back and forth, I have to explicitly specify the child class type for each WizardManager..

So.. (Controller)

public MyController : WizardManager < MainModel >...
public PartialViewResult SubsectionPage1(MainModel mdl)..

(View)

@model MainModel
CustName:  @Html.TextBoxFor(m => m.CustomerPersonalDetails.CustName) 

..looks like it works, but data isn't retained.. however..

(Controller)

public MyController : WizardManager< PersonalDetails >...
public PartialViewResult SubsectionPage1(PersonalDetails details)..

(View)

@model PersonalDetails
CustName:  @Html.TextBoxFor(m => m.CustName) 

Works just fine... is there something I'm missing why the MVCWizard.Wizard can't seem to handle "complex" models which hold one or more "child" types? Ultimately I can code it so I pass these smaller models to the WizardManager and then combine and map them across to my domain model before persisting to the datastore, but I'd be grateful for an explanation..

* EDIT *

Ok, I've dug around a bit more and that initial model binding should work just fine within most normal circumstances. But when using MVCWizard.Wizard, the issue seems to be with the "EnableSimpleMerge" feature of the MVCWizard.Wizard. Its used during the page navigation and there seems to be an issue with how that merge function uses reflection to discover the properies within the model. From my initial look, it doesn't appear to be coded to recurse through and "discover" child objects within a more complex model structure..

private void Merge(T wizard, T model)
        {
            foreach (PropertyInfo info in model.GetType().GetProperties())
            {
                object obj2 = info.GetGetMethod().Invoke(model, null);
                if (obj2 != null)
                {
                    wizard.GetType().GetProperty(info.Name).GetSetMethod().Invoke(wizard, new object[] { obj2 });
                }
            }
            foreach (FieldInfo info3 in model.GetType().GetFields())
            {
                object obj3 = info3.GetValue(model);
                if (obj3 != null)
                {
                    wizard.GetType().GetField(info3.Name).SetValue(wizard, obj3);
                }
            }
        }

解决方案

(See comment above :)

A more detailed Merge method needed to be written using indepth property reflection

这篇关于MVCWizard.Wizard - 使用复杂模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆