【发布时间】:2019-07-19 02:59:10
【问题描述】:
我设计了一个实现下拉列表类的 ASP.NET Core Razor Pages 应用程序,并使用该类作为创建、读取和更新类的基础。
现在我想实现匿名身份验证,并为此创建了另一个类,理想情况下它应该是 Create、Read 和 Update 类的基类。当我尝试添加它时,系统说我不能使用 2 个基类。
如何在 ASP.NET Core Razor (MVVM) 中使用多个基类
我尝试使用这两个类,但触发了一个错误,指出我不能使用多个基类
我的下拉列表基类
public class GLRefPageModel: PageModel
{
public SelectList GLRefNameSL { get; set; }
public void PopulateGLRefDropDownList(strMaterialsTransactContext _context, object selectedGLRef = null)
{
var GLRefsQuery = from d in _context.GLRef
select d;
GLRefNameSL = new SelectList(GLRefsQuery.AsNoTracking(), "ID", "Description", selectedGLRef);
}
}
我的身份验证基类
public class DI_BasePageModel : PageModel
{
protected ApplicationDbContext Context { get; }
protected IAuthorizationService AuthorizationService { get; }
protected UserManager<IdentityUser> UserManager { get; }
public DI_BasePageModel(
ApplicationDbContext context,
IAuthorizationService authorizationService,
UserManager<IdentityUser> userManager) : base()
{
Context = context;
UserManager = userManager;
AuthorizationService = authorizationService;
}
}
我的编辑类
public class EditModel : GLRefPageModel
{
private readonly strMaterialsTransact.Models.strMaterialsTransactContext _context;
public EditModel(strMaterialsTransact.Models.strMaterialsTransactContext context)
{
_context = context;
}
[BindProperty]
public strMovement strMovement { get; set; }
public async Task<IActionResult> OnGetAsync(int? id)
{
if (id == null)
{
return NotFound();
}
if (strMovement == null)
{
return NotFound();
}
//select the current GLRef
PopulateGLRefDropDownList(_context, strMovement.GLRefID);
return Page();
}
public async Task<IActionResult> OnPostAsync(int? id)
{
if (!ModelState.IsValid)
{
return Page();
}
var strMovementToUpdate = await _context.strMovement.FindAsync(id);
if (await TryUpdateModelAsync<strMovement>(
strMovementToUpdate,
"strmovement", //prefix for form value
s => s.ID, s => s.TransactionDate, s => s.QtyFromStore, s => s.IDPartNbr,
s => s.QtyToStore, s => s.GLRefID, s => s.ShopOrder, s => s.TransactionReason, s => s.TransactionReason,
s => s.OwnerID, s => s.TimeLastAccessed, s => s.Initials, s => s.LastUser))
{
await _context.SaveChangesAsync();
return RedirectToPage("./Index");
}
//**Select GLRef if TryUpdateModelAsync fails
PopulateGLRefDropDownList(_context, strMovementToUpdate.GLRefID);
return Page();
}
}
我希望我能够在我的创建、更新和读取操作中调用我的下拉列表操作的基类,并且还能够调用(和使用)该类进行匿名身份验证练习
【问题讨论】:
-
你不能。这是语言限制。 C# 只支持单继承。您必须使用不同的策略,例如组合。
-
我知道 c# 不允许多类继承,所以这不是我要寻找的答案。我正在尝试解决这个问题。我需要一个真正经验丰富的 c# 开发人员,因为我是中级......
-
构图或装饰。这几乎是主要的选择。
-
嗨,克里斯,你能给我一些例子吗?请记住,我已经有了这两个类,所以如果我需要做一些调整,你的示例应该指出这一点。
-
它们都是有据可查的模式。做一些研究,然后尝试一些东西。如果您遇到问题/卡住了,那么您可以提出一个具体的问题。
标签: c# asp.net-core razor-pages