I'm learning Razor Pages and building a project but stuck somewhere. Simply, I want user to select only 2 sports form checkboxes. There are total 4 sport checkbox and must be selected only 2 of them or program should give error.
I have a Student table that has FirstBranch and SecondBranch.Selected 2 checkboxes should go there.
Here is my Register.cshtml file (front-side)
@page
@using StudentRegisterProject.Web.Model
@model StudentRegisterProject.Web.Pages.Student.RegisterModel
@{
}
<h1>Sport Registration Form</h1>
<hr />
<div class="container">
<form method="post">
<div class="mb-3">
<label for="name" class="form-label">Name</label>
<input type="text" class="form-control" id="name">
</div>
<div class="mb-3">
<label for="email" class="form-label">E-Mail</label>
<input type="email" class="form-control" id="email">
</div>
<div class="form-check mb-2">
<input class="form-check-input" type="checkbox" asp-for="Branches.Football">
<label class="form-check-label">
Football
</label>
</div>
<div class="form-check mb-2">
<input class="form-check-input" type="checkbox" asp-for="Branches.Basketball">
<label class="form-check-label">
Basketball
</label>
</div>
<div class="form-check mb-2">
<input class="form-check-input" type="checkbox" asp-for="Branches.Volleyball">
<label class="form-check-label">
Volleyball
</label>
</div>
<div class="form-check mb-2">
<input class="form-check-input" type="checkbox" asp-for="Branches.Tennis">
<label class="form-check-label">
Tennis
</label>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
And this is the Register.cshtml.cs file (model file)
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Mvc.Rendering;
using StudentRegisterProject.Web.Data;
using StudentRegisterProject.Web.Model;
namespace StudentRegisterProject.Web.Pages.Student
{
[BindProperties]
public class RegisterModel : PageModel
{
private readonly StudentDbContext _studentDbContext;
public RegisterModel(StudentDbContext studentDbContext)
{
_studentDbContext = studentDbContext;
}
public Model.Student Student { get; set; }
public Branch Branches { get; set; }
public void OnGet()
{
}
public void OnPost()
{
}
}
I got the values in Branches property but need to give condition to them and can't use it.
And also my Student.cs model file in database
namespace StudentRegisterProject.Web.Model
{
public class Student
{
public Guid Id { get; set; }
public string Name { get; set; }
public string Email { get; set; }
public string FirstBranch { get; set; }
public string SecondBranch { get; set; }
}
}
And this is Branch.cs file
namespace StudentRegisterProject.Web.Model
{
public class Branch
{
public string Football { get; set; }
public string Basketball { get; set; }
public string Tennis { get; set; }
public string Volleyball { get; set; }
}
}
I don't know my approach is wrong or not? I hold the true/false values from checkboxes in Braches property but don't know how to give condition to them like if Branches.Football is true, Student.FirstBranch = "Football".