Search code examples
htmlasp.netsyntaxmodelrazor-pages

How to Display Value From Separate Table on Razor Page Create.cshtml


I have a Create page to create data for my Supervisor model. It looks like this: Create Supervisor Snip

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 Lists, IEnumerables but I just can't figure out the syntax on the cshtml page.

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.

One thing I tried on the .cshtml page was:

@{
    var result = (from x in @Model.DisplayCommitteeData where x.CommitteeNo == 1 select x);
    {
        return result.ToString();
    }
}

Bur I keep getting an error on the word 'return' and I'm not sure what may go there instead.

Thanks in advance for any and all assistance.


Solution

  • OK, after trying many different things for what is a simple answer (but still not simple for ME!), I hit upon that the label just like the field itself really needs an @foreach. Then I was able to use the IEnumerable I already had in place and use the following on my view page above each committee field:

                            @foreach (var item1 in Model.DisplayCommitteeData)
                            if (item1.CommitteeNo == 1)
                            {
                                @item1.CommitteeDesc;
                            }
    

    I simply changed 'item1.CommitteeNo == 1) to 'item2.CommitteeNo ==2' etc for each of the committee fields. I then removed the label asp-for code lines as they were no longer needed.