Search code examples
c#oopconstructorblazorblazor-editform

How do I reference a constructor with parameters using EditForm C#?


I'm trying to create a one to many relationship. A Company should be able to have many Boards. I have set up my model with such a relationship. However, I'm not sure how I call the correct constructor? as I initialize the object using the EditForm on my Create_Company page.

Company

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>();
        }
    }

I know how to do this using:

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

But not my current way of doing it:

Create_Company

@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();

    private async void HandleValidSubmit()
    {
        try
        {
            var response = await Http.PostAsJsonAsync("api/Companies", Company);
            response.EnsureSuccessStatusCode();

            await response.Content.ReadAsStringAsync();
            Navigation.NavigateTo("/read_companies");
        }
        catch (Exception)
        {

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

Solution

  • Your issue seems to be not how to call constructor, but when. You could use a new object when the user submits. Then use the new company object.

        private async void HandleValidSubmit()
        {
            try
            {
                Company newCompany = new Company(Company.Name, Company.Founded);
    
                var response = await Http.PostAsJsonAsync("api/Companies", newCompany);
                response.EnsureSuccessStatusCode();
    
                await response.Content.ReadAsStringAsync();
                Navigation.NavigateTo("/read_companies");
            }
            catch (Exception)
            {
    
                Console.WriteLine("Exception occurred for POST company");
            }
        }