0

我的申请应基于以下内容

用户创建一个公司,公司内部是一个董事会列表。然后,用户创建一个董事会,将其添加到公司内部的董事会列表中。就像一对多的关系。

我的问题

我不确定如何实现这个?

这是我目前拥有的。(在 Create_Board 上按提交后由于冻结而无法工作)。

公司.cs

public class Company
    {
        [Key]
        public Guid Id { get; set; }

        [Required]
        public string Name { get; set; }

        public DateTime Founded { get; set; }

        public List<Board> Boards { get; set; }

        public Company()
        {
        }

        public Company(string name, DateTime founded)
        {
            Name = name;
            Founded = founded;
            Boards = new List<Board>();
        }
    }

Create_Company.razor

@page "/create_company"

@inject NavigationManager Navigation
@inject HttpClient Http

<div>
    <button class="btn btn-outline-secondary oi oi-arrow-left" @onclick="GoToReadCompany"></button>
    <h3 class="text-center">Create Company</h3>
</div>

<hr />

<hr />

<EditForm Model="Company" OnValidSubmit="@HandleValidSubmit">
    <ObjectGraphDataAnnotationsValidator />

    <div class="form-group row">
        <label for="Name" class="col-sm-2 col-form-label">Name</label>
        <div class="col-sm-10">
            <InputText id="Name" class="form-control" placeholder="Name" @bind-Value="Company.Name" />
            <ValidationMessage For="@(() => Company.Name)" />
        </div>
    </div>

    <div class="form-group row">
        <label for="CompanyFounded" class="col-sm-2 col-form-label">Founded</label>
        <div class="col-sm-10">
            <InputDate id="CompanyFounded" class="form-control" @bind-Value="Company.Founded" />
            <ValidationMessage For="@(() => Company.Founded)" />
        </div>
    </div>

    <hr />

    <div class="row justify-content-md-center">
        <button type="submit" class="btn btn-primary">Submit</button>
    </div>
</EditForm>

@code {
    private void GoToReadCompany()
    {
        Navigation.NavigateTo("/read_companies");
    }

    private Company Company { get; set; } = new Company("", new DateTime());

    private async void HandleValidSubmit()
    {
        try
        {
            Console.WriteLine("VALID SUBMIT!");

            Company newCompany = new Company(Company.Name, Company.Founded);

            var response = await Http.PostAsJsonAsync("api/Companies", newCompany);
            response.EnsureSuccessStatusCode();

            Navigation.NavigateTo("/read_companies");
        }
        catch (Exception)
        {

            Console.WriteLine("Exception occurred for POST company");
        }
    }
}

Board.cs

public class Board
    {
        [Key]
        public Guid Id { get; set; }

        [ValidateComplexType]
        public Company Company { get; set; }

        [Required]
        public Guid CompanyId { get; set; }

        [Required]
        public string Name { get; set; }

        public string Description { get; set; }

        public List<Ticket> Tickets { get; set; }

        public Board()
        {
        }

        public Board(string name, string description, Company company)
        {
            Name = name;
            Description = description;
            Company = company;
            Tickets = new List<Ticket>();
        }
    }

Create_Board.razor

@page "/create_board"

@inject NavigationManager Navigation
@inject HttpClient Http

<div>
    <button class="btn btn-outline-secondary oi oi-arrow-left" @onclick="GoToHome"></button>
    <h3 class="text-center">Create Board</h3>
</div>

<hr />

<EditForm Model="Board" OnValidSubmit="@HandleValidSubmit">
    <ObjectGraphDataAnnotationsValidator />

    <div class="form-group row">
            <label for="Company" class="col-sm-2 col-form-label">Company</label>
            <div class="col-sm-10">
                <InputSelect id="Company" class="form-control" @bind-Value="Board.Company">
                    <option value="" disabled selected>Company</option>
                    @foreach (var company in Companies)
                    {
                        <option value="@company">@company.Name</option>
                    }
                </InputSelect>
                <ValidationMessage For="@(() => Board.Company)" />
            </div>
        </div>

    <div class="form-group row">
        <label for="Name" class="col-sm-2 col-form-label">Name</label>
        <div class="col-sm-10">
            <InputText id="Name" class="form-control" placeholder="Name" @bind-Value="Board.Name" />
            <ValidationMessage For="@(() => Board.Name)" />
        </div>
    </div>

    <div class="form-group row">
        <label for="Description" class="col-sm-2 col-form-label">Description</label>
        <div class="col-sm-10">
            <InputText id="Description" class="form-control" placeholder="Description" @bind-Value="Board.Description" />
            <ValidationMessage For="@(() => Board.Description)" />
        </div>
    </div>

    <hr />

    <div class="row justify-content-md-center">
        <button type="submit" class="btn btn-primary">Submit</button>
    </div>
</EditForm>

        @code {
            private void GoToHome()
            {
                Navigation.NavigateTo("/");
            }

            private Board Board { get; set; } = new Board("","", new Company());

            private List<Company> Companies = new List<Company>();

            protected override async Task OnInitializedAsync()
            {
                try
                {
                    Companies = await Http.GetFromJsonAsync<List<Company>>("api/Companies");
                }
                catch (Exception)
                {
                    Console.WriteLine("Exception occurred for GET companies");
                }
            }

            private async void HandleValidSubmit()
            {
                try
                {
                    Console.WriteLine("VALID SUBMIT!");

                    Board newBoard = new Board(Board.Name, Board.Description, Board.Company);

                    var response = await Http.PostAsJsonAsync("/api/Boards", newBoard);
                    response.EnsureSuccessStatusCode();

                    Navigation.NavigateTo($"/");
                }
                catch (Exception)
                {

                    Console.WriteLine("Exception occurred for POST board");
                }
            }
        }

4

0 回答 0