示例#1
0
        // 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()
        {
            //var emptyStudent = new Student();

            //if (await TryUpdateModelAsync<Student> (
            //        emptyStudent,
            //        "student",
            //        s => s.FirstMidName, s=> s.LastName, s=> s.EnrollmentDate)){
            //    _context.Students.Add(emptyStudent);
            //    await _context.SaveChangesAsync();
            //    return RedirectToPage("./Index");
            //}
            //return Page();

            //SetValues 方法通过从另一个 PropertyValues 对象读取值来设置此对象的值。 SetValues 使用属性名称匹配。
            //视图模型类型不需要与模型类型相关,它只需要具有匹配的属性。
            //使用 StudentVM 时需要更新 Create.cshtml 才能使用 StudentVM 而非 Student。

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

            var entry = _context.Add(new Student());

            entry.CurrentValues.SetValues(StudentVM);
            await _context.SaveChangesAsync();

            return(RedirectToPage("./Index"));

            {
            }
        }
        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            var entry = _context.Add(new Student());

            entry.CurrentValues.SetValues(StudentVM);
            await _context.SaveChangesAsync();

            return(RedirectToPage("./Index"));
        }
        public async Task <IActionResult> OnPostAsync()
        {
            //W/o StudentVM this function is vulnerable to overposting attacks
            //Overposting = adding extra fields to the POST request
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            var entry = _context.Add(new Student());

            //This ensures only expected fields are updated
            entry.CurrentValues.SetValues(StudentVM);
            await _context.SaveChangesAsync();

            return(RedirectToPage("./Index"));
        }
示例#4
0
        //[BindProperty]
        // public StudentVM StudentVM { get; set; }


        // 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 <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            var entry = _context.Add(new Student());

            entry.CurrentValues.SetValues(Student);
            await _context.SaveChangesAsync();

            return(RedirectToPage("./Index"));


            /*  TryUpdateModel
             *
             * var emptyStudent = new Student();
             *
             * if (await TryUpdateModelAsync<Student>(
             * emptyStudent,
             * "student",   // Prefix for form value.
             * s => s.FirstMidName, s => s.LastName, s => s.EnrollmentDate))
             * {
             * _context.Students.Add(emptyStudent);
             * await _context.SaveChangesAsync();
             * return RedirectToPage("./Index");
             * }
             *
             * return Page();
             *
             * /* original
             *
             * if (!ModelState.IsValid)
             * {
             *   return Page();
             * }
             *
             * _context.Students.Add(Student);
             * await _context.SaveChangesAsync();
             *
             * return RedirectToPage("./Index");
             */
        }
        // 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()
        {
            //if (!ModelState.IsValid)
            //{
            //    return Page();
            //}

            //_context.Students.Add(Student);
            //await _context.SaveChangesAsync();

            //return RedirectToPage("./Index");

            /*
             *
             *
             */

            //var emptyStudent = new Student();

            //if (await TryUpdateModelAsync<Student>(
            //emptyStudent,
            //    "student",   // Prefix for form value.
            //    s => s.FirstMidName, s => s.LastName, s => s.EnrollmentDate))
            //{
            //    _context.Students.Add(emptyStudent);
            //    await _context.SaveChangesAsync();
            //    return RedirectToPage("./Index");
            //}

            //return Page();

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

            var entry = _context.Add(new Student());

            entry.CurrentValues.SetValues(Student);
            await _context.SaveChangesAsync();

            return(RedirectToPage("./Index"));
        }
        // 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 <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            // _context.Students.Add(Student);
            // await _context.SaveChangesAsync();

            //var emptyStudent = new Student ();

            //使用 PageModel 中 PageContext 属性的已发布的表单值
            //过多发布
            // if (await TryUpdateModelAsync<Student> (
            //         emptyStudent,
            //         "student", // Prefix for form value.
            //         //仅更新列出的属性
            //         s => s.FirstMidName, s => s.LastName, s => s.EnrollmentDate)) {

            //     _context.Students.Add (emptyStudent);

            //     await _context.SaveChangesAsync ();

            //     return RedirectToPage ("./Index");
            // }

            var entry = _context.Add(new Student());

            //SetValues 方法通过从另一个 PropertyValues 对象读取值来设置此对象的值。 SetValues 使用属性名称匹配。
            //视图模型类型不需要与模型类型相关,它只需要具有匹配的属性。
            entry.CurrentValues.SetValues(StudentVM);

            await _context.SaveChangesAsync();

            return(RedirectToPage("./Index"));
        }