当前位置: 首页 > 知识库问答 >
问题:

无法在剃刀页窗体的选择列表中保存多个选择

柳镜
2023-03-14

在这里,我可以绑定多选择的选择列表。当我试图保存时,我没有得到一个错误,但它没有保存官员信息。

下面是试图加载页面时出现的错误

处理请求时发生未处理的异常。SQLException:无效的列名'OfficerID'。Microsoft.data.sqlclient.sqlCommand+<>c.b__164_0(任务结果)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using SecurityCore.Models;
using System.ComponentModel.DataAnnotations;

namespace SecurityCore.Pages.SecurityLogs
{
    public class IndexModel : PageModel
    {
        private readonly SecurityCore.Models.SecurityCoreContext _context;

        public IndexModel(SecurityCore.Models.SecurityCoreContext context)
        {
            _context = context;
        }


        public string EventDateSort { get; set; }
        public string EventStartSort { get; set; }
        public string EventEndSort { get; set; }
        public string ContactNameSort { get; set; }
        public string EventTypeSort { get; set; }
        public string ShiftRangeSort { get; set; }
        public string EntitySort { get; set; }
        public string LocationSort { get; set; }
        public string NarrativeSort { get; set; }
        public string OfficerNameSort { get; set; }
        public string FullNameSort { get; set; }            
        public string SubjectDOBSort { get; set; }
        public string RecordLockedSort { get; set; }
        public string CurrentFilter { get; set; }
        public string CurrentSort { get; set; }
        public string IDSort { get; set; }

        public Nullable<DateTime> dateEnd { get; set; }      
        public Nullable<DateTime> dateBegin { get; set; }


        [TempData]
        public string Message { get; set; }

        public bool ShowMessage => !string.IsNullOrEmpty(Message);
        public PaginatedList<SecurityLog> SecurityLog { get; set; }            


        public async Task OnGetAsync(string sortOrder, string currentFilter, string searchString, int? pageIndex, string entitySelect)
        {

            ViewData["EntityID"] = new SelectList(_context.Entity.Where(a => a.Active == "Y"), "ID", "Name");

            CurrentSort = sortOrder;
            IDSort = sortOrder == "ID" ? "ID_Desc" : "ID";
            EventDateSort = sortOrder == "EventDate" ? "EventDate_Desc" : "EventDate";                
            ContactNameSort = sortOrder == "ContactName" ? "ContactName_Desc" : "ContactName";
            EventTypeSort = sortOrder == "EventType" ? "EventType_Desc" : "EventType";
            ShiftRangeSort = sortOrder == "ShiftRange" ? "ShiftRange_Desc" : "ShiftRange";
            EntitySort = sortOrder == "Entity" ? "Entity_Desc" : "Entity";
            LocationSort = sortOrder == "Location" ? "Location_Desc" : "Location";
            NarrativeSort = sortOrder == "Narrative" ? "Narrative_Desc" : "Narrative";
            FullNameSort = sortOrder == "FullName" ? "FullName_Desc" : "FullName";                
            RecordLockedSort = sortOrder == "Locked" ? "NotLocked" : "Locked";
            OfficerNameSort = sortOrder == "OfficerName" ? "OfficerName_Desc" : "OfficerName";

            if (searchString != null)
            {
                pageIndex = 1;
            }
            else
            {
                searchString = currentFilter;
            }

            CurrentFilter = searchString;


            IQueryable<SecurityCore.Models.SecurityLog> sort = from s in _context.SecurityLog select s;

            if (!String.IsNullOrEmpty(searchString))
            {
                sort = sort.Where(s =>    s.Narrative.Contains(searchString)                                                                             
                                       || s.RecordLocked.Contains(searchString)
                                       || s.EventDate.ToString().Contains(searchString)                                                                                  
                                       || s.ContactName.Contains(searchString)
                                       || s.Entity.Name.Contains(searchString)
                                       || s.Location.Name.Contains(searchString)
                                       || s.EventType.Name.Contains(searchString)
                                       || s.ShiftRange.Name.Contains(searchString)
                                       || s.ID.ToString().Contains(searchString)
                                       || s.SubjectFirst.Contains(searchString)
                                       || s.SubjectLast.Contains(searchString)
                                       || s.Officer.FullName.Contains(searchString)                                           
                );
            }

            switch (sortOrder)
            {
                case "ID_Desc":
                    sort = sort.OrderByDescending(s => s.ID);
                    break;
                case "ID":
                    sort = sort.OrderBy(s => s.ID);
                    break;
                case "EventDate":
                    sort = sort.OrderBy(s => s.EventDate);                    
                    break;                
                case "ContactName":
                    sort = sort.OrderBy(s => s.ContactName).ThenBy(s => s.EventDate);
                    break;
                case "ContactName_Desc":
                    sort = sort.OrderByDescending(s => s.ContactName).ThenBy(s => s.EventDate);
                    break;
                case "ShiftRange":
                    sort = sort.OrderBy(s => s.ShiftRange.Name).ThenBy(s => s.EventDate);
                    break;
                case "ShiftRange_Desc":
                    sort = sort.OrderByDescending(s => s.ShiftRange.Name).ThenBy(s => s.EventDate);
                    break;
                case "EventType":
                    sort = sort.OrderBy(s => s.EventType.Name).ThenBy(s => s.EventDate);
                    break;
                case "EventType_Desc":
                    sort = sort.OrderByDescending(s => s.EventType.Name).ThenBy(s => s.EventDate);
                    break;
                case "Entity":
                    sort = sort.OrderBy(s => s.Entity.Name).ThenBy(s => s.EventDate);
                    break;
                case "Entity_Desc":
                    sort = sort.OrderByDescending(s => s.Entity.Name).ThenBy(s => s.EventDate);
                    break;
                case "Location":
                    sort = sort.OrderBy(s => s.Location.Name).ThenBy(s => s.EventDate);
                    break;
                case "Location_Desc":
                    sort = sort.OrderByDescending(s => s.Location.Name).ThenBy(s => s.EventDate);
                    break;
                case "Narrative":
                    sort = sort.OrderBy(s => s.Narrative).ThenBy(s => s.EventDate);
                    break;
                case "Narrative_Desc":
                    sort = sort.OrderByDescending(s => s.Narrative).ThenBy(s => s.EventDate);
                    break;                                    
                case "NotLocked":
                    sort = sort.OrderBy(s => s.RecordLocked).ThenBy(s => s.EventDate);
                    break;
                case "Locked":
                    sort = sort.OrderByDescending(s => s.RecordLocked).ThenBy(s => s.EventDate);
                    break;
                case "OfficerName":
                    sort = sort.OrderBy(s => s.Officer.FullName).ThenBy(s => s.EventDate);
                    break;
                case "OfficerName_Desc":
                    sort = sort.OrderByDescending(s => s.Officer.FullName).ThenBy(s => s.EventDate);
                    break;
                default:
                    sort = sort.OrderByDescending(s => s.EventDate);
                    break;
            }

            int pageSize = 12;

            SecurityLog = await PaginatedList<SecurityLog>.CreateAsync(sort
                .Include(a => a.Entity)
                .Include(b => b.EventType)
                .Include(c => c.Location)
                .Include(d => d.ShiftRange)
                .Include(e => e.Officer)
                .Include(f => f.SecurityLogOfficer)                    
                .AsNoTracking(), pageIndex ?? 1, pageSize);           

        }       

    }
}


//The Security Log Model

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel;
using ExpressiveAnnotations.Attributes;

namespace SecurityCore.Models
{   
    public class SecurityLog
    {        
        public int ID { get; set; }

        [DataType(DataType.Date)]
        [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")]
        [Required]
        [Display(Name = "Event Date")]           
        public System.DateTime EventDate { get; set; }


        [Required]
        [Display(Name = "Shift Range")]       
        //[ForeignKey("ShiftRange")]
        public Nullable<int> ShiftRangeID { get; set; }

        [Required]
        //[ForeignKey("EventType")]
        [Display(Name = "Event Type")]
        public Nullable<int> EventTypeID { get; set; }

        [Required]
        [Display(Name = "Event Start")]
        [DataType(DataType.DateTime)]
        [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy  hh:mm}")]
        public System.DateTime EventStart { get; set; }            

        [Required]
        [Display(Name = "Event End")]
        [DataType(DataType.DateTime)]        
        [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy  hh:mm}")]
        public System.DateTime EventEnd { get; set; }

        [Required]
        [Display(Name = "Reporting Person")]
        public string ContactName { get; set; }

        [Required]
        //[ForeignKey("Entity")]
        [Display(Name = "Entity")]
        public Nullable<int> EntityID { get; set; }

        [Required]
        //[ForeignKey("Location")]
        [Display(Name = "Location")]
        public Nullable<int> LocationID { get; set; }

        [Required]
        public string Narrative { get; set; }

        //Set length of narrative displayed
        private int NarrativeLimit = 200;
        [Display(Name = "Narrative")]
        public string NarrativeTrimmed
        {
            get
            {
                if (Narrative.Length > this.NarrativeLimit)
                    return this.Narrative.Substring(0, this.NarrativeLimit) + "...";
                else
                    return this.Narrative;
            }
        }

        [Required]
        [Display(Name = "Subject's First Name")]
        public string SubjectFirst { get; set; }

        [Required]
        [Display(Name = "Subject's Last Name")]
        public string SubjectLast { get; set; }


        [Display(Name = "Subject's Name")]
        public string FullName
        {
            get
            {
                return SubjectFirst + " " + SubjectLast;
            }            
        }

        [Display(Name = "Subject's B#/DOB")]
        public string SubjectDOB { get; set; }

        private string _RecordLocked;

        [Display(Name = "Record Locked?")]
        public string RecordLocked 
        {
            get { return _RecordLocked; }
            set
            {
                _RecordLocked = value;
                if (_RecordLocked == "Y")
                {
                    try { 
                    }
                    catch(Exception ex)
                    {

                    }
                }
            }        
        }


        [Display(Name = "Entered By")]
        public string EnteredBy { get; set; }

        [Display(Name = "Create Date")]
        [ReadOnly(true)]
        [DataType(DataType.Date)]
        public Nullable<System.DateTime> CreateDate { get; set; }

        [Display(Name = "Modified Date")]        
        [DataType(DataType.DateTime)]
        public Nullable<System.DateTime> ModifiedDate { get; set; }                

        [Display(Name = "Modified By")]
        public string ModifiedBy { get; set; }

        [Display(Name ="Number of Extinguishers")]        
        public Nullable<int> ExtinguisherNo { get; set; }

        [Display(Name ="Total Blankets")]
        public Nullable<int> BlanketNo { get; set; }

        [Display(Name ="Cause of Alarm")]
        public string FireAlarmCause { get; set; }

        [Display(Name ="Doors Closed in Area?")]
        public string DoorsClosed { get; set; }

        [Display(Name ="Number of Staff Responding")]
        public Nullable<int> StaffNo { get; set; }

        public virtual Entity Entity { get; set; }
        public virtual ShiftRange ShiftRange { get; set; }
        public virtual EventType EventType { get; set; }
        public virtual Location Location { get; set; }
        public virtual Officer Officer { get; set; }
        public virtual SecurityLogOfficer SecurityLogOfficer { get; set; }        

    }


}

//The Officer Model

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;

namespace SecurityCore.Models
{
    public class Officer
    {
        [Required]
        public int ID { get; set; }

        //[Required]
        [Display(Name = "Officer's First Name")]
        public string FirstName { get; set; }

        //[Required]
        [Display(Name = "Officer's Last Name")]
        public string LastName { get; set; }

        [Display(Name = "Officer's Name")]
        public string FullName
        {
            get
            {
                return FirstName + " " + LastName;
            }
        }

        [Required]
        public string Active { get; set; }            
        public string LoginName { get; set; }     
        public virtual SecurityLogOfficer SecurityLogOfficer { get; set; }

    }
}

//The SecurityLogOfficer Model

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;

namespace SecurityCore.Models
{
    public class SecurityLogOfficer
    {
        [Required]
        public int ID { get; set; }

        [Required]
        public int SecurityLogID { get; set; }

        public int OfficerID { get; set; }                 
    }
}



//The Create Page that I am trying to post from…

using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Mvc.Rendering;
using SecurityCore.Models;

namespace SecurityCore.Pages.SecurityLogs
{
    public class CreateModel : PageModel
    {
        private readonly SecurityCore.Models.SecurityCoreContext _context;

        public CreateModel(SecurityCore.Models.SecurityCoreContext context)
        {
            _context = context;
        }

        public IActionResult OnGetAsync()
        {              
            ViewData["EntityID"] = new SelectList(_context.Entity.Where(a=>a.Active == "Y"), "ID", "Name");
            ViewData["ShiftRangeID"] = new SelectList(_context.ShiftRange.Where(a=>a.Active == "Y"), "ID", "Name");
            ViewData["LocationID"] = new SelectList(_context.Location.Where(a=>a.Active == "Y"), "ID", "Name");
            ViewData["EventTypeID"] = new SelectList(_context.EventType.Where(a=>a.Active == "Y"), "ID","Name");
            ViewData["Officer"] = new SelectList(_context.Officer.Where(a => a.Active == "Y"), "ID", "FullName");

            return Page();
        }

        [TempData]
        public string Message { get; set; }

        [BindProperty]
        public SecurityLog SecurityLog { get; set; }

        [BindProperty(SupportsGet = true)]
        public int entity { get; set; }
        public int eventType { get; set; }


        SelectList FilteredLocation;
        SelectList FilteredEventType;

        public JsonResult OnGetLocations()
        {


            FilteredLocation = new SelectList(_context.Location.Where(c => c.EntityID == entity).Where(c =>c.Active == "Y").OrderBy(c =>c.Name), "ID", "Name");

            return new JsonResult(FilteredLocation);


        }

        public JsonResult OnGetEventTypes()
        {


            FilteredEventType = new SelectList(_context.EventType.Where(c => c.EntityID == entity).Where(c => c.Active == "Y").OrderBy(c => c.Name), "ID", "Name");

            return new JsonResult(FilteredEventType);     

        }               


        // To protect from overposting attacks, please enable the specific properties you want to bind to, for
        // more details see https://aka.ms/RazorPagesCRUD.
        public async Task<IActionResult> OnPostAsync()
        {
            ModelState.Remove("EnteredBy");
            ModelState.Remove("ModifiedDate");
            ModelState.Remove("CreateDate");
            ModelState.Remove("ModifiedBy");
            ModelState.Remove("RecordLocked");


            if (!ModelState.IsValid)
            {
                return Page();
            }

            SecurityLog.EnteredBy = User.Identity.Name;
            SecurityLog.ModifiedDate = DateTime.Now;
            SecurityLog.CreateDate = DateTime.Now;
            SecurityLog.ModifiedBy = User.Identity.Name;
            SecurityLog.RecordLocked = "N";

            _context.SecurityLog.Add(SecurityLog);
            await _context.SaveChangesAsync();

            Message = "Entry added successfully!";

            return RedirectToPage("Index");

        }



    }
}

共有1个答案

鲁鸿
2023-03-14

根据错误描述,Officer表只有ID列。它没有OfficerID列。尝试更改代码以反映ID列。

 类似资料:
  • 本文向大家介绍选择在HTML中的剃刀Html.DropDownListFor()的页面加载上选择了哪个选项?,包括了选择在HTML中的剃刀Html.DropDownListFor()的页面加载上选择了哪个选项?的使用技巧和注意事项,需要的朋友参考一下 速记用于确定在剃刀Html.DropDownListFor()的页面加载上选择了哪个选项。您可以尝试运行以下代码片段-

  • 我试图保存一个表单,该表单在选择标记中有一个外键,但它总是为空。当我使用inspect元素时,它会发布选定的id,所以我不能说这是表单的问题。有人能帮忙吗。形式 控制器 根本原因 java.lang.ClassNotFoundException:org.hibernate.exception.locktimeoutException org.apache.catalina.loader.webap

  • 问题内容: 我想选择,,从多个选择其中有10个选项。我只想选择这三个选项。 HTML代码: selenium键代码: 我尝试使用此代码。使用此代码,我可以选择第一个选项,即“ P0_ENGLISH”。但是,选择第一个选项后,我得到一个错误: 问题答案: 要从 Multi Select 元素中选择多个 选项 ,可以使用 ActionChains 模拟 Control单击* ,如下所示: *

  • 我正在使用具有多重选择和分页功能的datatable,当我选择一行并转到另一个页面,然后返回到我所在的页面时,我选择的行将不再被选中。我使用的是primefaces 3.5、mojarra、jboss 7.1,我的bean是ViewScope。下面是我的代码: 型号:

  • 我正在尝试自动选择硒离子中的下拉列表,但我无法使其正常工作。 基本上,我在菜单项上记录了一次单击,这会显示下拉菜单,但每当我在其中一个选项上使用单击命令时,它都会关闭菜单,而不会选择新选项。我也尝试了select命令,但我一直得到“指定元素不是select” 任何想法?

  • 本文向大家介绍如何在R中选择列表的多个元素?,包括了如何在R中选择列表的多个元素?的使用技巧和注意事项,需要的朋友参考一下 通常,R中的列表包含大量元素,每个元素可以具有不同的类型,这对于列表来说是一件好事。由于我们可以将数据类型存储为列表元素,因此存储和选择不同类型的数据变得更加容易。我们还可以一次选择列表中的单个或多个元素。这可以借助单个方括号来完成。 示例 请看以下列表- 选择list_da