我有以下抽象类:

public abstract class TemplateBase
{
    public abstract string TemplateName { get; }
    public string RuntimeTypeName { get { return GetType().FullName; } }
    public abstract List<AreaContainer> TemplateAreas { get; }
}


然后这两个继承的类:

public class SingleColumnTemplate : TemplateBase
{
    public override string TemplateName { get { return "Single column"; } }

    public AreaContainer CenterColumn { get; private set; }

    public SingleColumnTemplate()
    {
        this.CenterColumn = new AreaContainer("Middle");
    }

    private List<AreaContainer> templateAreas;

    public override List<AreaContainer> TemplateAreas
    {
        get
        {
            if (this.templateAreas == null)
            {
                this.templateAreas = new List<AreaContainer>() { this.CenterColumn };
            }

            return this.templateAreas;
        }
    }
}




public class TwoColumnTemplate : TemplateBase
{
    public override string TemplateName { get { return "Two column"; } }
    public AreaContainer LeftColumn { get; private set; }
    public AreaContainer RightColumn { get; private set; }

    public TwoColumnTemplate()
    {
        LeftColumn = new AreaContainer("Left");
        RightColumn = new AreaContainer("Right");
    }

    private List<AreaContainer> templateAreas;

    public override List<AreaContainer> TemplateAreas
    {
        get
        {
            if (this.templateAreas == null)
            {
                this.templateAreas = new List<AreaContainer>() { this.LeftColumn, this.RightColumn };
            }
            return this.templateAreas;
        }
    }
}


我也有这个类,这是我的编辑模型:

public class ContentPage
{
    public virtual int ContentPageId { get; set; }

    public virtual string Title { get; set; }

    public TemplateBase Template { get; set; }
}


题:

对于我的ActionResult,我具有以下内容:

[HttpGet]
public ActionResult Edit()
{
    var row = new ContentPage();
    var template = new TwoColumnTemplate();

    // Areas
    HtmlArea html_left = new HtmlArea();
    html_left.HtmlContent = "left area html content";

    HtmlArea html_right = new HtmlArea();
    html_right.HtmlContent = "right area html content";

    template.LeftColumn.Areas.Add(html_left);
    template.RightColumn.Areas.Add(html_right);

    row.Template = template;
    return View(row);
}

[HttpPost]
[ValidateInput(false)]
public ActionResult Edit(ContentPage row)
{
    // Here i could loop through List -TemplateAreas and save each template Area to Db. I guess that would work

    return this.View(row);
}


题:

对于HttpGet,我如何从数据库加载行模板?因为它可能是SingleColumnClass或TwoColumnClass。

我的ViewModel如何解决这个问题?

谢谢

最佳答案

您需要知道控制器中的模板类型,以便可以将视图中的参数传递给控制器​​,以指示类型(SingleColumnTwoColumn)。您可以使用Enum进行此操作:

public enum TemplateType
{
   SingleColumn,
   TwoColumn
}

[HttpGet]
public ActionResult Edit(TemplateType templateType)
{
    var row = new ContentPage();
    TemplateBase template;

    if (templateType == TemplateType.SingleColumn)
    {
        template = new SingleColumnTemplate();
    }
    else
    {
        template = new TwoColumnTemplate();
    }

    ...

    return View(row);
}


从视图创建操作链接时,可以指定:

<%= Html.ActionLink("Edit",
                    "Edit",
                    "YouController",
                    new
                    {
                        // singlecolumn or twocolumn
                        // depending on your concrete view
                        TemplateType = TemplateType.xxx
                    },
                    null);

关于c# - .net从抽象类创建对象实例,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4592925/

10-13 06:46