How can I turn a text input separated by comma in html into a list of a model that I created and add the values into a sql-table
Models
public class Person
{
[Key]
public string PersonId { get; set; }
public string Name { get; set; }
[DefaultValue(0)]
public int Set1 { get; set; }
[DefaultValue(0)]
public int Set2 { get; set; }
[DefaultValue(0)]
public int Set3 { get; set; }
public string DepartmentId { get; set; }
public Department Department { get; set; }
}
public class Department
{
[Key]
public string DepartmentId { get; set; }
public string? DepartmentName { get; set; }
public List<Person> Persons { get; set; }
}
View Model
public class CreateDepartmentViewModel
{
public string DepartmentName { get; set; }
public List<Person> Persons { get; set; }
}
Cshtml Page
@model BTAnCaWeb.ViewModels.CreateDepartmentViewModel
<div class="container mb-3">
<div class="row pt-4">
<div class="col-6">
<h2 class="text-primaary">
Thêm phòng ban mới
</h2>
</div>
</div>
</div>
<form method="post" action="Create">
<div class="mb-3">
<label for="DepName" class="form-label">Department Name</label>
<input type="text" class="form-control" asp-for="@Model.DepartmentName" id="DepName">
</div>
<div class="mb-3">
<label for="Persons" class="form-label">People in department</label>
<input type="text" class="form-control" asp-for="@Model.Persons" id="Persons" />
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
I haven't found anything relevant on stack overflow or google yet
Split(',')
to seperate the value by comma.