I am working on an MVC application. And I want to create employees. When I create employees I add a rank between 1-10. And I want this rank to multiply with a constant salary coefficent. For example, I create an employee with the rank of 5. When creating this, I want the rank of 5 to multiply with an constant salary coefficent on, for example 1.125.
My Model:
public class Employee
{
public int Id { get; set; }
[DisplayName("First Name")]
[Required]
public string FirstName { get; set; }
[DisplayName("Last Name")]
[Required]
public string LastName { get; set; }
[DisplayName("Rank")]
[Range(1, 10)]
[Required]
public decimal Salary { get; set; }
public bool IsCEO { get; set; }
public bool IsManager { get; set; }
public int ManangerId { get; set; }
}
and here is my create function in my controller
public async Task<IActionResult> AddEmployee([Bind("Id,FirstName,LastName,Salary,IsCEO,IsManager,ManangerId")] Employee employee)
{
//Employee CalculateSalary = _context.Employees.FirstOrDefault(
// s => s.Salary == CalculateSalary.Salary);
if (ModelState.IsValid)
{
_context.Add(employee);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
return View(employee);
}
Any idea on how I could do this? Could this be done in my controller or something else? Maybe some kind of helper class?
public async Task<IActionResult> AddEmployee([Bind("Id,FirstName,LastName,Salary,IsCEO,IsManager,ManangerId")] Employee employee)
{
//Added this code
var coefficent = (employee.IsCEO) ? 2.725 : (employee.IsManager) ? 1.725 : 1.125;
employee.Salary = Convert.ToDecimal(coefficent) * employee.Salary;
if (ModelState.IsValid)
{
_context.Add(employee);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
return View(employee);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.