【解決方法】MVCを使用して主キーフィールドにボタン(次の前の最初の最後の)ナビゲーションボタンに機能を追加する方法


==========問題==========

リポジトリを使用した作成アクションの次の前の最後の最初に関数を追加できません

EmployeeId フィールドに基づくパターン

==========例==========

従業員 ID : 最後のレコードとして 5

IF 作成アクション ビューが読み込まれると、従業員 ID の最大値 + 1 を表示する必要があります。つまり、6 と表示されます。
IF Click NextButton が 6 になる

IF Click previousButton が 4 になり、名前の年齢などに関連する別のデータを取得します…

IF Click LastButton が 5 になり、名前の年齢などに関連する別のデータを取得します…

IF Click FirstButton が 1 になり、名前の年齢などに関連する別のデータを取得します…

=======使用技術============

私はビジュアルスタジオ2017 asp.netコア2.1 SQLサーバー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 は、任意の IDENTITY に対して EF によって割り当てられます / [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()

コメント

タイトルとURLをコピーしました