簡體   English   中英

我如何確認控制器中的操作刪除(POST)?

[英]How do i confirm the action delete(POST) in my controller?

我有一個 ViewModel,我想進行功能刪除(GET)和 deleteConfirmed(POST),這樣我就可以刪除存儲在我的數據庫中的所有數據

我不知道也想知道要采取什么步驟來完成 deleteConfirmed。 通常有自動生成的代碼,但這不是我需要的。

這是我的 ViewModel

using System;
using ExploFormsDB.Models;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

namespace ExploFormsDB.ViewModels
{
    public class WorkShiftDetailViewModel
    {
        [Key]
        public int WorkShiftId { get; set; }
        public int? HoleId { get; set; }
        public string HoleName { get; set; }
        public int SurveyLocationId { get; set; }
        public int SupplierId { get; set; }
        public int ZoneId { get; set; }
        public string SurveyLocation1 { get; set; }
        public string  SupplierName { get; set; }
        public string ZoneName { get; set; }
        public DateTime StartDay { get; set; }
        public DateTime EndDay { get; set; }

        public ICollection<WorkerViewModel> WorkShiftEmployees { get; set; }
    }
} 

這是我的控制器,我包含了 Create 以幫助更好地理解。 GET:刪除似乎工作正常,我在處理帖子時遇到了問題。 任何幫助都會做什么。 如果問題已經得到回答,請給我一個鏈接。 我對 c# 和核心很陌生,對 ViewModels 完全陌生


        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Create(WorkShiftDetailViewModel workShiftDetailViewModel)
        {

            if (!ModelState.IsValid)
            {
                WorkShift ws = new WorkShift();
                ws.StartDay = workShiftDetailViewModel.StartDay;
                ws.EndDay = workShiftDetailViewModel.EndDay;
                ws.SupplierId = workShiftDetailViewModel.SupplierId;
                ws.SurveyLocationId = 1;
                ws.ZoneId = workShiftDetailViewModel.ZoneId;
                ws.HoleId = workShiftDetailViewModel.HoleId;
                _context.Add(ws);
                await _context.SaveChangesAsync();

                foreach (WorkerViewModel member in workShiftDetailViewModel.WorkShiftEmployees)
                {
                    if (member.isDeleted == false) {
                        WorkShiftTeam emp = new WorkShiftTeam();
                        emp.EmployeeId = member.EmployeeId;
                        emp.RoleId = member.RoleId;
                        emp.WorkShiftId = ws.WorkShiftId;
                        _context.Add(emp);
                    }
                }
                HttpContext.Session.SetInt32("wsId", ws.WorkShiftId);

                await _context.SaveChangesAsync();

                return RedirectToAction(nameof(CreateSharedView));
            }

            return View(workShiftDetailViewModel);
        }

         public IActionResult Delete(int? id)
        {
            if (id == null)
            {
                return NotFound();
            }

            List<WorkerViewModel> Workers = new List<WorkerViewModel>();
            WorkShift ws = _context.WorkShift.Include(w => w.WorkShiftTeam).SingleOrDefault(x => x.WorkShiftId == id);
            WorkShiftDetailViewModel detail = new WorkShiftDetailViewModel();
            detail.HoleName = ws.HoleId == null ? "N/A" : _context.Hole.Find(ws.HoleId).HoleName;
            detail.StartDay = ws.StartDay;
            detail.EndDay = ws.EndDay;
            detail.ZoneName = _context.Zone.Find(ws.ZoneId).ZoneName;
            detail.SurveyLocation1 = _context.SurveyLocation.Find(ws.SurveyLocationId).SurveyLocation1;
            detail.SupplierName = _context.Supplier.Find(ws.SupplierId).SupplierName;
            detail.WorkShiftId = ws.WorkShiftId;

            int order = 0;

            var rolelist = new SelectList(_context.Role, "RoleId", "Role1");

            var empsWithFullName = from e in _context.Employee.Where(a => a.IsActive)
                                   select new
                                   {
                                       ID = e.EmployeeId,
                                       FullName = e.LastName + ", " + e.FirstName
                                   };
            var empList = new SelectList(empsWithFullName, "ID", "FullName");

            foreach (WorkShiftTeam member in ws.WorkShiftTeam.OrderBy(a => a.EmployeeId))
            {
                Workers.Add(new WorkerViewModel() { EmployeeId = member.EmployeeId, RoleId = member.RoleId, Index = order, Roles = rolelist, Employees = empList });
                order++;
            }
            detail.WorkShiftEmployees = Workers;

            return View(detail);
        }

        // POST: WorkShiftDetailViewModels/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> DeleteConfirmed(int id)
        {
            //??
        } ```

為什么你為刪除操作創建了一個額外的方法作為 HttpGet? (發生沖突)

將其更改為:

[HttpGet]
public IActionResult GetById(int? id) { ... }

並且只有一種具有此定義的刪除方法

[HttpPost]
public async Task<IActionResult> Delete(int? id) { ... }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM