如何使用 MVC 将功能添加到按钮(下一个、前一个、最后一个)导航按钮到主键字段

[ad_1]

==========问题==========

无法将函数添加到 Next previous last first 以使用存储库创建操作

基于 EmployeeId 字段的模式

==========示例==========

员工 ID : 5 作为最后记录

如果创建操作视图被加载,它必须显示 Employee Id 的 max+1,这意味着它将显示 6
IF 点击NextButton 变为6

IF 单击 previousButton 变为 4 并获取与姓名、年龄等相关的另一个数据…

IF 单击 LastButton 变为 5 并获取与姓名、年龄等相关的另一个数据…

IF 单击 FirstButton 变为 1 并获取与姓名、年龄等相关的另一个数据…

=======使用的技术============

我在 Visual Studio 2017 asp.net core 2.1 sql server 2012 中使用存储库模式通用工作

我尝试过的:

===========Employee Controller Have Create Action=====

public class EmployeesController : Controller
    {
        private readonly IEmployees _context;

        public EmployeesController(IEmployees context)
        {
            _context = context;
        }

        
        // GET: Employees/Create
        public IActionResult Create()
        {
How to call functions below
         //   var maxId =   maxid+1;
getNext()
getprevious()
getlast()
getfirst()
            return View();
        }
===============Generic Repository Pattern(required function I dont know======
 
 public class EFRepository<T> : IRepository<T> where T : class
    {
        protected TabDbContext _context { get; set; }
        public EFRepository(TabDbContext context)
        {
            _context = context;
        }
getNext(){
what implementation write
}
getprevious()
{
what implementation write
}
getlast()
{
what implementation write
}
getfirst()
{
what implementation write
}
public IQueryable<T> GetAll()
        {
            return _context.Set<T>();
        }

        public virtual async Task<ICollection<T>> GetAllAsyn()
        {

            return await _context.Set<T>().ToListAsync();
        }

        public virtual T Get(int id)
        {
            return _context.Set<T>().Find(id);
        }

        public virtual async Task<T> GetAsync(int id)
        {
            return await _context.Set<T>().FindAsync(id);
        }

       
        public virtual T Find(Expression<Func<T, bool>> match)
        {
            return _context.Set<T>().SingleOrDefault(match);
        }

        public virtual async Task<T> FindAsync(Expression<Func<T, bool>> match)
        {
            return await _context.Set<T>().SingleOrDefaultAsync(match);
        }

        public ICollection<T> FindAll(Expression<Func<T, bool>> match)
        {
            return _context.Set<T>().Where(match).ToList();
        }

        public async Task<ICollection<T>> FindAllAsync(Expression<Func<T, bool>> match)
        {
            return await _context.Set<T>().Where(match).ToListAsync();
        }

}
=====Buttons Navigation on Create View Of Employee Controller====
<div class="title_of_div">
    <button id="BtnFirst" style="display:inline">First</button>
    <button id="BtnNext" style="display:inline">Next</button>
    <button id="BtnPrevious" style="display: inline">Previous</button>
    <button id="BtnLast" style="display: inline">Last</button>
</div>
view create controlls
===========Create Action Get==========
 <form asp-action="Create">

            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
            <div class="form-group">
                <label asp-for="EmployeeId" class="control-label"></label>
                <input asp-for="EmployeeId" class="form-control" />
                <span asp-validation-for="EmployeeId" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="BranchCode" class="control-label"></label>
                <input asp-for="BranchCode" class="form-control" />
                <span asp-validation-for="BranchCode" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="EmployeeName" class="control-label"></label>
                <input asp-for="EmployeeName" class="form-control" />
                <span asp-validation-for="EmployeeName" class="text-danger"></span>
            </div>
            
        </form>
    </div>
</div>
============Model related data=============
public class Employee
    {
        [Key]
        public int EmployeeId { get; set; }
        public int BranchCode { get; set; }
        public string EmployeeName { get; set; }
   
    }

解决方案1

1)“创建”不需要id; id 由 EF 为任何 IDENTITY / 分配 [Key] 字段/属性。

2) 下一步: ..Where( e => e.Id > 5 ).Orderby( e => e.Id ).FirstOrDefault()

3) 上一个: ..Where( e => e.Id < 5 ).Orderby( e => e.Id ).LastOrDefault()

4) 首先:..Orderby( e => e.Id ).FirstOrDefault()

5) 最后一个: ..Orderby( e => e.Id ).LastOrDefault()

[ad_2]

コメント

标题和URL已复制