I want to make countries using HierarchyId type so
I created a Country
model class with these properties:
namespace DotNetCore5Crud.Models
{
public class Country
{
public int Id { get; set; }
[Required]
public string Name { get; set; }
public HierarchyId parentId { get; set; }
}
}
I then created an Index
view which works fine:
@model IEnumerable<Category>
<partial name="_addCategory" model="new Category()" />
@{
if (!Model.Any())
{
<div class="alert alert-warning" role="alert">
there Is no Categories
</div>
}
else
{
<div class="container">
<table class="table table-sm table-hover table-striped">
<thead>
<tr class="bg-primary text-white font-weight-bold">
<th>id</th>
<th>Name</th>
<th>HierarchyId</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>@item.Id</td>
<td>@item.Name</td>
<td>@item.hierarchyId</td>
</tr>
}
</tbody>
</table>
</div>
}
}
Then, I inject a partial view to AddCountry
:
@model Category
<!-- Button trigger modal -->
<button type="button" class="btn btn-sm mb-2 btn-primary" data-toggle="modal" data-target="#exampleModalCenter">
<i class="bi bi-plus-circle"></i> Add Category
</button>
<!-- Modal -->
<div class="modal fade" id="exampleModalCenter" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
<form method="post" asp-action="Create" asp-controller="Categories">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLongTitle"><i class="bi bi-plus-circle"></i> Add Category</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<div class="form-group">
<label class="text-muted" asp-for="Name"></label>
<input type="text" asp-for="Name" class="form-control" />
<span class="text-danger" asp-validation-for="Name"></span>
</div>
<div class="form-control">
<label class="text-muted" asp-for="hierarchyId"></label>
<select class="form-control" asp-for="hierarchyId" id="hierarchyId">
@foreach (var item in ViewBag.AllCAT)
{
<option value="@item.Id">@item.Name</option>
}
</select>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-sm btn-secondary" data-dismiss="modal">Cancel</button>
<button type="submit" class="btn btn-sm btn-primary">Save</button>
</div>
</div>
</div>
</form>
</div>
And finally, when I send data from the view, the name is sent correctly but the HierarchyId
column is null like this :
I do not know why that is the case... I have searched a lot and have not yet found an explanation
I'd appreciate it if you can tell my why this is the case.