我有一個多步驟的嚮導,我從喜歡this它有幾個問題有用的帖子拼湊但是..這裏的設置我有ModelBinding - 模型動作之間通過成形後的重建 - 如何堅持
[Serializable]
public class WizardModel
{
public IList<IStepViewModel> Steps
{
get;
set;
}
public void Initialize()
{
Steps = typeof(IStepViewModel)
.Assembly
.GetTypes()
.Where(t => !t.IsAbstract && typeof(IStepViewModel).IsAssignableFrom(t))
.Select(t => (IStepViewModel)Activator.CreateInstance(t))
.ToList();
}
}
我的嚮導控制器
public ActionResult Index()
{
var wizard = new WizardModel();
wizard.Initialize();
//this populates wizard.Steps with 3 rows of IStepViewModel
return View(rollover);
}
[HttpPost]
public ActionResult Index(
[Deserialize] WizardModel wizard,
IStepViewModel step
)
{
//but when this runs wizard is a new class not the one previously Initialized
wizard.Steps[rollover.CurrentStepIndex] = step;
}
我的問題是,嚮導是一個新的對象,每次貼出的 - 當我試着g圍繞填充數組中的每個步驟傳遞相同的模型。有沒有人知道我要去哪裏錯了?
這裏的ModelBinding
的Global.asax
ModelBinders.Binders.Add(typeof(IStepViewModel), new FormTest.Models.StepViewModelBinder());
,並提前
public class StepViewModelBinder : DefaultModelBinder
{
protected override object CreateModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Type modelType)
{
var stepTypeValue = bindingContext.ValueProvider.GetValue("StepType");
var stepType = Type.GetType((string)stepTypeValue.ConvertTo(typeof(string)), true);
var step = Activator.CreateInstance(stepType);
bindingContext.ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(() => step, stepType);
return step;
}
}
感謝
編輯:
如果我明白,使用會話的替代方法是序列化我的模型(下面),並在我的控制器操作中反序列化。我在發佈到控制器的模型上設置了這些值..這些值將返回到下一步的視圖等等,直到最後一步當我有一個嚮導模型填充每個步驟。
Index.cshtml
@using (Html.BeginForm())
{
@Html.Serialize("wizard", Model);
etc...
}
,讓我試試嚮導參數反序列化在這裏
[Deserialize] WizardModel wizard,
來通過控制器動作後每次一個新對象 - 我想看看這個有可能不使用會話,但@ Html.Serialize?和Post
感謝您的答覆。我試圖找出@ Html.Serialize是否可行?我在頂部添加了更多信息。 – MikeW