我正在使用Razor视图引擎来呈现一些HTML,然后这些HTML将存在于XML文档中。我正在使用的基类有许多属性,以及一个静态方法,它将返回该对象的列表(使用Dapper填充列表)。我在执行该方法时遇到问题,因为它需要返回基类,这是一个抽象类。下面是一些示例代码。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Dapper;
using System.Data.SqlClient;
using System.Configuration;
using System.ComponentModel;
using System.IO;
namespace LocalBranchesPOC
{
public abstract class PersonData : TemplateBase
{
#region Properties
public string RecordId { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public string City { get; set; }
public string County { get; set; }
public string State { get; set; }
public string Country { get; set; }
public string Zip { get; set; }
public string Phone { get; set; }
public string Variable1 { get; set; }
public string Variable2 { get; set; }
public string Variable3 { get; set; }
#endregion
public static List<PersonData> GetPeople()
{
const string QUERY = "SELECT [RecordId], [Name], [Address], [City], [County], [State], [Country], [Zip], [Phone], [Variable1], [Variable2], [Variable3] FROM Data.Person";
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["BranchLocator"].ConnectionString))
{
return getPeople(QUERY, conn);
}
}
private static List<PersonData> getPeople(string QUERY, SqlConnection conn)
{
conn.Open();
var result = conn.Query<PersonData>(QUERY).ToList();
conn.Close();
return result;
}
}
public abstract class TemplateBase
{
[Browsable(false)]
public StringBuilder Buffer { get; set; }
[Browsable(false)]
public StringWriter Writer { get; set; }
public TemplateBase()
{
Buffer = new StringBuilder();
Writer = new StringWriter(Buffer);
}
public abstract void Execute();
// Writes the results of expressions like: "@foo.Bar"
public virtual void Write(object value)
{
// Don't need to do anything special
// Razor for ASP.Net does HTML encoding here.
WriteLiteral(value);
}
// Writes literals like markup: "<p>Foo</p>"
public virtual void WriteLiteral(object value)
{
Buffer.Append(value);
}
}
}
基本上我对PersonData.GetPeople()
的调用失败,因为PersonData类是抽象的。任何想法将不胜感激。我正在使用here中的示例作为我的向导。
答案 0 :(得分:0)
您正在尝试合并模型和视图 不要那样做;它不可能奏效。
相反,将模型作为单独的属性传递给视图,也许将其加载到TemplateBase
构造函数中。