How to I properly implement C# classes using MVC ASP.NET?

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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>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}");
}
}
}
</code>
<code>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}"); } } } </code>
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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>@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>
</code>
<code>@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> </code>
@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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>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);
}
}
}
</code>
<code>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); } } } </code>
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 🙂

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật