jaabh
jaabh

Reputation: 1004

ASP.NET Core Edit action does not update new info

I have calculations that are currently in model, as I am trying to find an efficient place to store my calculations, here is the model

public class Worker
{
    public int Id {get;set;}
    public string Name {get;set;}
    public double Q1Rev {get;set;}
    public double Q2Rev {get;set;}
    public double Q1Cost {get;set;}
    public double Q2Cost {get;set;}

    public double Q1Profit{
       get
       {
           return Q1Rev - Q1Cost;
       }
    }
    public double Q2Profit
    {
        get
        {
            return Q2Rev - Q2Cost;
        }
    }
}

In my Edit action of my controller, if I only want to edit the Q1Rev for example, it allows me to do so however my Q1Profit and Q2Profit will now be set to '0' instead of preserving the original or the actual number Q1Profit = Q1Rev - Q1Cost. Instead it is just sets Q1Profit = 0. Here is the edit controller

    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> Edit(int id, [Bind("Id,UserId,Q1Rev,Q2Rev,Q1Cost,Q2Cost,Q1Profit,Q2Profit")] Worker worker)
    {
        if (id != worker.Id)
        {
            return NotFound();
        }

        if (ModelState.IsValid)
        {
            try
            {
                _context.Update(worker);
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!WorkerExists(worker.Id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }
            return RedirectToAction(nameof(Index));
        }
        return View(worker);
    }

How can I change it so everytime I Create a new one or Edit an already existing property it calculates everything properly? So it calculates Q1Profit = Q1Rev - Q1Cost after I edit one of the values. Instead I want it to save my new values and actually perform the calculation for the new Q1Profit.

Upvotes: 0

Views: 366

Answers (1)

jaabh
jaabh

Reputation: 1004

I found that in the controller if i want to save the new values and perform the new calculations, I need to add specifically the values that I am adding in the controller like so

    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> Edit(int id, [Bind("Id,UserId,Q1Rev,Q2Rev,Q1Cost,Q2Cost,Q1Profit,Q2Profit")] Worker worker)
    {
        if (id != worker.Id)
        {
            return NotFound();
        }

        if (ModelState.IsValid)
        {
            try
            {
                var employee = await _context.Workers.SingleOrDefaultAsync(w => w.Id == worker.Id);
                employee.Q1Rev = worker.Q1Rev;
                employee.Q2Rev = worker.Q2Rev;
                employee.Q1Cost = worker.Q1Cost;
                employee.Q2Cost = worker.Q2Cost;


                _context.Update(employee);
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!WorkerExists(worker.Id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }
            return RedirectToAction(nameof(Index));
        }
        return View(worker);
    }

This way allows me to edit only the values I want while still preserving the originals and calculations, if anyone can explain why this way works and the other doesn't I would appreciate it.

Upvotes: 1

Related Questions