HeyLameRobin
HeyLameRobin

Reputation: 331

How to update all rows of a table in database using one Action method (ASP.NET)?

I want to create a button such that if I click on it, it changes the entire column of a table in database. Please consider the code below:

Model:

using Player_Simulation.Data;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

namespace Player_Simulation.Models
{
    public class Player
    {
        public int PlayerId { get; set; }
        public string PlayerName { get; set; }
        public int PlayerRating { get; set; }
        public int PlayerYear { get; set; }
        public int PlayerSkill { get; set; }
        public string PlayerCountry { get; set; }
        public ICollection<Match> Matches { get; set; } 

        public Player()
        {
            PlayerYear = 1;
            PlayerRating = 1000;
        }
    }
}

Controller:

namespace Player_Simulation.Controllers
{
    public class PlayerController : Controller
    {
        private readonly DataContext _database;

        public PlayerController(DataContext database)
        {
            _database = database;
        }

        [HttpPost]
        public void IncrementYear()
        {
            using (_database)
            {
                foreach (Player player in _database.Players)
                {
                    if (player != null)
                    {
                        int oldYear = player.PlayerYear;
                        player.PlayerYear = oldYear + 1;
                        _database.SaveChanges();
                    }
                }
            }
        }
    }
}

View:

// removed code for shorter post
// the button to click so I can increment the 'year' column by 1
<a asp-controller="Player" asp-action="IncrementYear" class="btn btn-info">Increment Year</a>

Graphics: enter image description here

Ultimately, I want a button that can increment all the years by 1. It does not need to return a View or anything like that. However, after referring to: stackoverflow and codegrepper.com, I get an error 405. Please see the Action method above on PlayerController called IncrementYear for my attempt.

How do I properly update all these rows of data such that all the years are incremented? I would appreciate any help!

Upvotes: 0

Views: 1418

Answers (1)

Xinran Shen
Xinran Shen

Reputation: 9963

You use [httppost] to receive a post request in your IncrementYear ation, So you need send a post request in your view.

change the button like :

<form method="post">
<button asp-controller="Player" asp-action="IncrementYear" class="btn btn-info">Increment Year</button>
</form>

controller

public class PlayerController : Controller
    {
        private readonly TableContext _context;
        public PlayerController(TableContext tablecontext)
        {
            _context = tablecontext;
        }

        [HttpPost]
        public IActionResult IncrementYear()
        {
            foreach (var play in _context.players.ToList())
            {
                play.PlayerYear++;
                _context.SaveChanges();
            }

            return RedirectToAction("Privacy","Home");
        }
    }

enter image description here

Upvotes: 1

Related Questions