Czy mogę zrobić coś takiego?Utworzono nowy CustomModelBinder z tego, który już działał. Dlaczego nowy nigdy nie zostaje wezwany do zrobienia żadnego wiązania?
[HttpPost]
public ActionResult Index(WizardViewModel wizard, IStepViewModel step)
{
Gdzie mam następujących w moim Global.asax.cs Application_Start
ModelBinders.Binders.Add(typeof(IStepViewModel), new StepViewModelBinder());
ModelBinders.Binders.Add(typeof(WizardViewModel), new WizardViewModelBinder());
Aktualizacja
Więc starałem się zobaczyć co jest nie tak. Oto mój nowy kod. Wygląda na to, że problem polega na tym WizardViewModel i jego segregatorze. Co "mówi" aplikacji o oczekiwaniu i nadchodzącym modelu Kreatora?
[HttpPost]
public ActionResult Index(WizardViewModel wizard)
{
Gdzie mam następujących w moim Global.asax.cs Application_Start
ModelBinders.Binders.Add(typeof(WizardViewModel), new WizardViewModelBinder());
Pełny kod Binder
namespace Tangible.Binders
{
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;
}
}
public class WizardViewModelBinder : DefaultModelBinder
{
protected override object CreateModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Type modelType)
{
var wizardValue = bindingContext.ValueProvider.GetValue("wizard");
if (wizardValue != null)
{
var wizardType = Type.GetType((string)wizardValue.ConvertTo(typeof(string)), true);
var wizard = Activator.CreateInstance(wizardType);
bindingContext.ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(() => wizard, wizardType);
return wizard;
}
else
{
var wizard = new Tangible.Models.WizardViewModel();
wizard.Initialize();
return wizard;
}
}
}
}
Czy to próbował? –
Tak, ale mogłem bardzo łatwo mieć wiele innych problemów, które powodują, że to nie działa. Zanim przejdę dalej, pomyślałem, że powinienem sprawdzić, czy to możliwe. –
Doug, jak pamiętam z twojego poprzedniego pytania na http://stackoverflow.com/questions/6834814/modelmetadata-custom-class-attributes-and-an-indescribable-question, WizardViewModel zawarł 'IList' jako atrybut . Czy tak jest nadal?Jeśli tak, to twój WizardViewModelBinder powinien prawdopodobnie obsłużyć także powiązanie dla potomnej klasy IStepViewModel. Jak sugerowali inni, opublikuj swój model kodu spoiwa. –
counsellorben