Я создал как студент , так и классы StudentWorker без проблем, а также представление (по большей части). Я уверен, что большинство проблем лежат в контроллере. Мне сложно понять структуру Core MVC Core MVC, и я просто пытаюсь завершить этот класс C#.
Я перечислю файлы проекта ниже:
Классы модели: < /p>
Код: Выделить всё
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 = 1 && value
Просмотр < /p>
@model FinalProject.Models.StudentWorker
@{
ViewBag.Title = "Create Student Worker";
}
Student Worker
Create Student Worker
Student ID
Student Name
Hourly Pay
Hours Worked
Calculate Weekly Salary
@if (ViewBag.WeeklySalary != null)
{
Weekly Salary: @ViewBag.WeeklySalary
}
< /code>
контроллер < /p>
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);
}
}
}
Подробнее здесь: https://stackoverflow.com/questions/787 ... t-core-mvc
Мобильная версия