Answer the question
In order to leave comments, you need to log in
Why ParentCategory=null?
The site has services that are divided into categories Category
model
public class Category : EntityBase
{
[Required]
public Guid Id { get; set; }
[Display(Name = "Услуги")]
public IEnumerable<Service> Services { get; set; }
}
public class Service : EntityBase
{
[Required]
public Guid Id { get; set; }
[Required]
[Display(Name = "Название")]
public override string H1 { get; set; }
[Display(Name = "Категория")]
public Category ParentCategory { get; set; }
}
public IActionResult Edit(Guid id)
{
var entity = id == default ? new Domain.Entities.Service() : dataManager.Services.GetServiceById(id);
ViewBag.Categories = new SelectList(dataManager.Categories.GetCategories(), "Id", "H1", entity.ParentCategory);
return View(entity);
}
<form asp-area="Admin" asp-controller="Services" asp-action="Edit" method="post" enctype="multipart/form-data">
<div>
<label asp-for="ParentCategory"></label>
<select asp-for="ParentCategory" asp-items="ViewBag.Categories"></select>
<span asp-validation-for="ParentCategory"></span>
</div>
Answer the question
In order to leave comments, you need to log in
Or write virtual for the given data model field.
public class Service : EntityBase
{
[Required]
public Guid Id { get; set; }
[Required]
[Display(Name = "Название")]
public override string H1 { get; set; }
[Display(Name = "Категория")]
public virtual Category ParentCategory { get; set; }
}
dataManager.Services.GetAll().Include(x => x.ParentCategory).FirstOrDefault(x => x.Id ==id);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question