I have a Create page to create data for my Supervisor model. It looks like this:
The Supervisor class is here:
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace Votes.Model
{
public class Supervisor
{
[Key]
public int SupervisorId { get; set; }
[Display(Name = "Supervisor Last Name")]
public string? SupLastName { get; set; }
[Display(Name = "Supervisor First Name")]
public string? SupFirstName { get; set; }
[Display(Name = "Ward")]
public int? Ward { get; set; }
[Display(Name = "District")]
public int? District { get; set; }
public ICollection<Committee>? Committees { get; set; }
[Display(Name = "Chair/Member Committee 1")]
public string? MemberComm1 { get; set; }
[Display(Name = " Chair/Member Committee 2")]
public string? MemberComm2 { get; set; }
}
}
public enum MemberComm1
{
C,
M,
X
}
public enum MemberComm2
{
C,
M,
X
}
Instead of the label for each MemberComm1 (Chair/Member Committee 1), MemberComm2 (Chair/Member Committee 2), etc, I would like to have the actual name of that committee (stored in the CommitteeDesc field).
Here is the Committee model:
using System.ComponentModel.DataAnnotations;
namespace Votes.Model
{
public class Committee
{
[Key]
public int CommitteeId { get; set; }
[Required]
[Display(Name = "Committee No")]
public int CommitteeNo { get; set; }
[Required]
[Display(Name = "Committee Description")]
public string? CommitteeDesc { get; set; }
[Required]
[Display(Name = "Committee Type")]
public string? CommitteeType { get; set; }
public ICollection<Supervisor> Supervisors { get; set; }
}
}
Since the Committees each have a fixed CommitteeNo assigned to them (1, 2, etc), I would like to use some type of if statement to say if Committee.CommitteeNo == 1, then CommitteeDesc. I would just like to use this as a label (ie, 1 = Finance Committee, 2 = Govt Operations, etc. It would not be committed to any database table.
I have tried a variety of @Html.DisplayNameFor and IEnumerables, but I just can’t figure out the syntax.
Here is the .cs for the Create page as it is now.
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.EntityFrameworkCore;
using Votes.Data;
using Votes.Model;
using System.Linq;
namespace Votes.Pages.Supervisors;
[BindProperties]
public class CreateModel : PageModel
{
private readonly ApplicationDbContext _db;
public Supervisor Supervisor { get; set; }
public Committee Committee { get; set; }
public IList<Committee> CommitteeList { get; set; }
public CreateModel(ApplicationDbContext db)
{
_db = db;
}
public IEnumerable<Committee> DisplayCommitteeData { get; set; }
public IEnumerable<Municipality> DisplayMunicipalityData { get; set; }
public IEnumerable<Ward> DisplayWardData { get; set; }
public IEnumerable<District> DisplayDistrictData { get; set; }
public async Task OnGet()
{
await _db.Committee.Select(a => a.CommitteeId).ToListAsync();
DisplayCommitteeData = await _db.Committee.ToListAsync();
await _db.Municipality.Select(a => a.MunicipalityId).ToListAsync();
DisplayMunicipalityData = await _db.Municipality.ToListAsync();
await _db.Ward.Select(a => a.WardId).ToListAsync();
DisplayWardData = await _db.Ward.ToListAsync();
await _db.District.Select(a => a.DistrictId).ToListAsync();
DisplayDistrictData = await _db.District.ToListAsync();
}
public async Task<IActionResult> OnPost()
{
if (ModelState.IsValid)
{
_db.Supervisor.Add(Supervisor);
await _db.Supervisor.AddAsync(Supervisor);
await _db.SaveChangesAsync();
TempData["success"] = "Supervisor added successfully.";
return RedirectToPage("Index");
}
return Page();
}
}
As you can see, I’ve got extra stuff I don’t likely need. All in my attempts to get this to work. Can anyone please describe the direction I should be going? A frequent error I am getting is that the object cannot be null. There is full Committee data in the Committee table.
Thanks in advance for any and all assistance.