The assignment is to create a MVC Web App that has a Student and a Student Worker class. You then have the user input the information for a Student Worker object (id, name, hourly pay, and hours worked). You output the weekly salary (hourlyPay * hoursWorked) to the View, making sure that the hourly pay is in the range (7.25 – 14.75), and the hours worked is in the range (1 – 15), I created both the Student and Student Worker class with no problems, as well as the View (for the most part). I’m pretty sure most of the issues lie in the Controller. The MVC ASP.NET framework is hard for me to understand, and I’m just trying to complete this C# class.
I’ll list the project files below:
Model
namespace FinalProject.Models
{
public class Student
{
// Private fields
private int id;
private string name;
// Public property for ID
public int ID
{
get { return id; }
set { id = value; }
}
// Public property for Name
public string Name
{
get { return name; }
set { name = value; }
}
// Constructors
public Student(int id, string name)
{
this.id = id;
this.name = name;
}
// Default constructor (demonstrates method overloading)
public Student()
{
id = 0;
name = "";
}
// Method to display student information (overrides default ToString method)
public override string ToString()
{
return ($"ID: {ID}, Name: {Name}");
}
}
/*
*/
public class StudentWorker : Student
{
// Private fields
private double hourlyPay;
private int hoursWorked;
// Public property for HourlyPay
public double HourlyPay
{
get { return hourlyPay; }
set
{
if (value >= 7.25 && value <= 14.75)
hourlyPay = value;
else
hourlyPay = 0;
}
}
// Public property for HoursWorked
public int HoursWorked
{
get { return hoursWorked; }
set
{
if (value >= 1 && value <= 15)
hoursWorked = value;
else
hoursWorked = 0;
}
}
// Constructor
public StudentWorker(int id, string name, double hourlyPay, int hoursWorked)
: base(id, name)
{
HourlyPay = hourlyPay;
HoursWorked = hoursWorked;
}
// Default constructor (demonstrates method overloading)
public StudentWorker()
: base()
{
HourlyPay = 0;
HoursWorked = 0;
}
// Method to calculate weekly salary
public double WeeklySalary()
{
if (HourlyPay == 0 || HoursWorked == 0)
return 0;
return HourlyPay * HoursWorked;
}
// Method to display student worker information (overrides default ToString method)
public override string ToString()
{
return ($"ID: {ID}, Name: {Name}, Hourly Pay: {HourlyPay:C}, Hours Worked: {HoursWorked}, Weekly Salary: {WeeklySalary():C}");
}
}
}
View
@model FinalProject.Models.StudentWorker
@{
ViewBag.Title = "Create Student Worker";
}
<html>
<head>
<title>Student Worker</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
</head>
<body>
<h2>Create Student Worker</h2>
<form asp-action="Index" method="post">
<div class="form-group">
<label asp-for="ID" class="control-label">Student ID</label>
<input asp-for="ID" class="form-control" />
<span asp-validation-for="ID" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Name" class="control-label">Student Name</label>
<input asp-for="Name" class="form-control" />
<span asp-validation-for="Name" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="HourlyPay" class="control-label">Hourly Pay</label>
<input asp-for="HourlyPay" class="form-control" />
<span asp-validation-for="HourlyPay" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="HoursWorked" class="control-label">Hours Worked</label>
<input asp-for="HoursWorked" class="form-control" />
<span asp-validation-for="HoursWorked" class="text-danger"></span>
</div>
<button type="submit" class="btn btn-primary">Calculate Weekly Salary</button>
</form>
@if (ViewBag.WeeklySalary != null)
{
<h3>Weekly Salary: @ViewBag.WeeklySalary</h3>
}
</body>
</html>
Controller
using FinalProject.Models;
using Microsoft.AspNetCore.Mvc;
namespace FinalProject.Controllers
{
public class HomeController : Controller
{
// GET: StudentWorker/Create
public IActionResult Index()
{
return View();
}
// POST: StudentWorker/Create
[HttpPost]
public IActionResult Index(StudentWorker model)
{
if (ModelState.IsValid)
{
// Calculate weekly salary
var weeklySalary = model.WeeklySalary();
ViewBag.WeeklySalary = weeklySalary;
// Return view with the weekly salary
return View(model);
}
// If model state is not valid, redisplay the form
return View(model);
}
}
}
I’ve tried removing the ranges for hourlyPay and hoursWorked, but that doesnt seem to do anything. Again, im unfamiliar with making Web Apps, and I would greatly appreciate any help 🙂