简体   繁体   English

如何在ASP.NET Razor页面中为我的CRUD操作使用多个基类

[英]How do I use multiple base classes for my CRUD operation in ASP.NET Razor pages

I designed an ASP.NET Core Razor Pages application implementing dropdown list class and using the class as a base for Create, Read and Update classes. 我设计了一个ASP.NET Core Razor Pages应用程序,它实现了下拉列表类,并使用该类作为Create,Read和Update类的基础。

Now I want to implement anonymous authentication and I have created another class for this which should ideally be the base class for the Create, Read and Update classes. 现在我想实现匿名身份验证,我已经为此创建了另一个类,理想情况下,它应该是Create,Read和Update类的基类。 When I tried to add it, the system says I cannot use 2 base classes. 当我尝试添加它时,系统说我不能使用2个基类。

How can I use multiple base classes in ASP.NET Core Razor (MVVM) 如何在ASP.NET Core Razor(MVVM)中使用多个基类

I tried using both classes but that triggered an error stating I cannot use more than one base class 我尝试使用这两个类但是触发了一个错误,指出我不能使用多个基类

My dropdown list base class 我的下拉列表基类

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);

        }
    }

My Authentication base class 我的身份验证基类

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;
        }
    }

My Edit Class 我的编辑课

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();
        }
    }

I expect to me able to call the base class for my dropdown list operation in my Create, Update and Read operations and also be able to call (and use) the class for the anonymous authentication exercise 我希望能够在我的创建,更新和读取操作中为我的下拉列表操作调用基类,并且还能够调用(和使用)该类进行匿名身份验证练习

I have been able to solve the problem by using composition design pattern all the classes into the DI_BasePageModel class as follows: 我已经能够通过使用组合设计模式将所有类放入DI_BasePageModel类来解决问题,如下所示:

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 SelectList GLRefNameSL { get; set; }

        public void PopulateGLRefDropDownList(ApplicationDbContext _context,     object selectedGLRef = null)
        {
            var GLRefsQuery = from d in _context.GLRef
                              select d;

            GLRefNameSL = new SelectList(GLRefsQuery.AsNoTracking(), "GLRefID", "Description", selectedGLRef);

        }

Thanks for pointing it out Chris Pratt 谢谢你指出克里斯普拉特

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ASP.Net Core 3.1 Razor Pages 和使用 Dapper 的 CRUD 操作 - ASP.Net Core 3.1 Razor Pages and CRUD operation with Dapper 如何提交表单,但我的文本框将其值保留在asp.net Razor页面中 - How do I submit a form, but my text boxes preserve their values in asp.net Razor pages 如何从ASP.Net网页和剃须刀上使用MongoDB? - How do I work with MongoDB from ASP.Net web pages and razor? 如何在ASP.Net Core 2.0 Razor页面中发布IFormFile? - How do I post an IFormFile in ASP.Net Core 2.0 Razor Pages? 如何在尝试使用 Razor Pages 删除 ASP.NET Core 中的记录时显示确认消息 - How do I display a confirmation message with trying to delete a record in ASP.NET Core using Razor Pages 如何在自定义构建的 ASP.NET Core Razor Pages 可编辑网格中发布行 ID? - How do I post the row id in a custom built ASP.NET Core Razor Pages editable Grid? 如何将 Razor Pages ASP.NET 应用程序连接到现有的非本地 MSSQL 数据库? - How do I connect a Razor Pages ASP.NET app to an already existing, not local MSSQL database? 如何为授权挑战设置两个不同的登录路径? (ASP.NET Core Razor 页面) - How do I set two different login paths for authorization challenge? (ASP.NET Core Razor Pages) 我无法在asp.net Core 2.0剃须刀页面中执行下载操作 - I'm unable to perform download operation in asp.net core 2.0 razor pages 如何在ASP.NET MVC应用程序中使用ASP.NET页? - How do I use ASP.NET pages in an ASP.NET MVC application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM