Click here to Skip to main content
15,886,258 members

Adding values in ASP.NET controller ​from the database SQL server column for a specific user

Member 14713380 asked:

Open original thread
My problem:

I need to add 'overtime' for all previous months from the beginning of the year to the month selected.

My table at sql server:

Overtime : varchar(20)
Id | Login | Year | Month | Day | Overtime
---+-------+------+-------+-----+----------
 1 | MIPA  | 2020 |   1   |  1  |  08:00
 2 | MIPA  | 2020 |   1   |  2  |  08:00
 3 | MIPA  | 2020 |   1   |  3  |  07:30
 4 | MIPA  | 2020 |   1   |  4  |  12:00
...
30 | MIPA  | 2020 |   1   |  30 |  04:00
...
41 | MIPA  | 2020 |   2   |  1  |  08:00
42 | MIPA  | 2020 |   2   |  2  |  08:00
43 | MIPA  | 2020 |   2   |  3  |  07:30
44 | MIPA  | 2020 |   2   |  4  |  12:00
...
52 | MIPA  | 2020 |   2   |  25 |  04:00

//so if the user chose March (3) and year: 2020(in dropdownlist). This function must extract the aggregated information from the overtime column of the previous months of the selected year. (that is, in the example for January (1) and February (2) and for 2020)

Controller:
[HttpPost]
public async Task<ActionResult> PartialTableEcp()  //change nothing
{
// Data is already extracted from other operations
// So e.g. user enters chooses year: 2020 month: March (3) 
int numberMonth= 3;
int numberYear= 2020;
int numberOfDays= 31;

// person identification by login
var userName = _httpContextAccessor.HttpContext.User.Identity.Name;

// retrieve all month records from the table, (i dont know it works)
var existingRecords = _tableEcpContext.Karta
                                      .Where(x => x.Login == userName && 
                                                  x.Year == numberYear && 
                                                  x.Month == numberMonth-1) ////will mathematical calculation work here?
                                      .Select(i => new{
                                                       i.Overtime
                                                       }).ToList(); 


and now there is a problem how to sum all these values ​​.. preferably in HHH: mm format


var thisValue = that this value would be written to the variable var;

var viewModel = new ParentView { Model1 = karta };
return PartialView("_TableViewEcp", viewModel); //change nothing

}

Model:

public partial class Karta_Model
    {
        [Key]
        public int Id { get; set; }
        public string? Login { get; set; }
        public int Year { get; set; }
        public int Month{ get; set; }
        public int? DayOfMonth { get; set; }
        public string? Overtime { get; set; }
     }

public partial class ParentView
    {
        public List<Karta_Model> Model1 { get; set; }
    }


What I have tried:

anyone have an idea?


what I wrote is at the top
Tags: C#, ASP.NET

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900