using _2021_backend.Models; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; using Microsoft.AspNetCore.Mvc.Rendering; using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Threading.Tasks; namespace _2021_backend.Pages.Sessions { public class CreateModel : PageModel { private readonly _2021_backend.Data.BackendContext Context; [Display(Name = "同时创建200和204场次")] [BindProperty] public bool CreateTwo { get; set; } [BindProperty] public Session InterviewTime { get; set; } [BindProperty] [Display(Name = "活动官")] public string Chiefint { get; set; } public List ChiefList { get; set; } public CreateModel(_2021_backend.Data.BackendContext context) { Context = context; } public async Task OnGet() { var qlst = from e in Context.Users select (new SelectListItem { Value = e.Id.ToString(), Text = e.Name, Selected = false, Disabled = false }); var lst = qlst.ToList().Distinct().ToList(); ChiefList = lst; return Page(); } // To protect from overposting attacks, enable the specific properties you want to bind to, for // more details, see https://aka.ms/RazorPagesCRUD. public async Task OnPostAsync() { if (!ModelState.IsValid) { return Page(); } InterviewTime.Students = new List(); InterviewTime.Chiefs = new List(); Session interviewTimeSecond = new Session(); if (CreateTwo) { string anotherPlace = InterviewTime.Place == "200" ? "204" : "200"; Session tmp = Context.Sessions.FirstOrDefault(r => r.Place == anotherPlace && r.Day == InterviewTime.Day && InterviewTime.BeginTime == r.BeginTime); if (tmp == null) { interviewTimeSecond = new Session() { Day = InterviewTime.Day, BeginTime = InterviewTime.BeginTime, Place = anotherPlace, Chiefs = new List(), SendSMS = InterviewTime.SendSMS, Students = new List(), Capacity = InterviewTime.Capacity }; Context.Add(interviewTimeSecond); Context.SaveChanges(); } } Context.Sessions.Add(InterviewTime); Context.SaveChanges(); return RedirectToPage("./Index"); } } }