C#MVC-通用向导表单
我正在尝试制作表单向导,在这里我可以定义步骤。对于每一个步骤,我需要有一个基于步骤类型的控件和视图。我尝试在泛型类型上执行此操作,但在控制器上运行泛型方法时遇到问题 下面是一个例子C#MVC-通用向导表单,c#,asp.net-mvc,C#,Asp.net Mvc,我正在尝试制作表单向导,在这里我可以定义步骤。对于每一个步骤,我需要有一个基于步骤类型的控件和视图。我尝试在泛型类型上执行此操作,但在控制器上运行泛型方法时遇到问题 下面是一个例子 public interface IExampleInterface { } public abstract class WizardBaseController<T> : Controller where T : IExampleInterface { public abstract List&
public interface IExampleInterface { }
public abstract class WizardBaseController<T> : Controller where T : IExampleInterface
{
public abstract List<T> Steps { get; set; }
public abstract ActionResult RenderStep(T step);
public virtual ActionResult RenderNext(T step)
{
var index = Steps.IndexOf(step);
return RenderStep(Steps[index+1]);
}
}
public class ExampleClass : IExampleInterface { }
public class WizardController<T> : WizardBaseController<ExampleClass> where T : ExampleClass
{
public override List<ExampleClass> Steps { get; set; }
public override ActionResult RenderStep(ExampleClass step)
{
//do stuff
throw new NotImplementedException();
}
public virtual ActionResult RenderStep(T step)
{
throw new NotImplementedException();
}
public ActionResult CreateSteps()
{
Steps = new List<ExampleClass>
{
new AnotherExampleClassA(),
new AnotherExampleClassB(),
new ExampleClass(),
new AnotherExampleClassB(),
new AnotherExampleClassA(),
};
return RenderNext(Steps.First());
}
}
public class AnotherExampleClassA : ExampleClass { }
public class ChildWizzardConotroller : WizardController<AnotherExampleClassA>
{
public override ActionResult RenderStep(AnotherExampleClassA e)
{
//do stuff
throw new NotImplementedException();
}
}
public class AnotherExampleClassB : ExampleClass { }
public class AnotherChildWizzardConotroller : WizardController<AnotherExampleClassB>
{
public override ActionResult RenderStep(AnotherExampleClassB e)
{
//do stuff
throw new NotImplementedException();
}
}
公共接口IExampleInterface{}
公共抽象类WizardBaseController:控制器,其中T:IExampleInterface
{
公共抽象列表步骤{get;set;}
公共抽象操作结果呈现步骤(T步骤);
公共虚拟操作结果RenderNext(T步)
{
var指数=步骤。IndexOf(步骤);
返回RenderStep(步骤[index+1]);
}
}
公共类ExampleClass:IExampleInterface{}
公共类WizardController:WizardBaseController其中T:ExampleClass
{
公共覆盖列表步骤{get;set;}
公共覆盖操作结果渲染步骤(ExampleClass步骤)
{
//做事
抛出新的NotImplementedException();
}
公共虚拟操作结果渲染步骤(T步骤)
{
抛出新的NotImplementedException();
}
公共操作结果CreateSteps()
{
步骤=新列表
{
新的AnotherExampleClassA(),
新的另一个示例ClassB(),
新示例类(),
新的另一个示例ClassB(),
新的AnotherExampleClassA(),
};
返回RenderNext(Steps.First());
}
}
公共类AnotherExampleClassA:ExampleClass{}
公共类ChildWizzardConotroller:向导控制器
我不确定我的做法是否正确
基本上,我的目标是创建具有不同类型步骤的向导,并根据一种类型的步骤调用不同的方法。例如,对于调用方法RenderStep(参数类型为-ExampleClass),我想调用WizardController中的方法,
当RenderStep(参数类型为-AnotherExampleClassA)调用ChildWizzardConotroller等中的方法时
<a href="@Url.Action("CreateSteps", "Wizard")">Trigger Action</a>