0

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!

2
  • so you inject a datacontext but then 'new' another one up? Commented Mar 10, 2022 at 1:48
  • @Jazb I see what you are saying. So instead I used the injected datacontext rather than 'new' another one. However, same result. Commented Mar 10, 2022 at 1:52

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

hahaha, sorry for my ugly page...
Your code works wonderful! I see the code, thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.