На моей странице я создаю несколько форм на основе моих моделей, например:
@for (var i = 0; i < Model.CaseData.Case.CaseSheets.Count; i++)
{
Model.CaseData.CaseSheetNum = i;
<div class="sheet @Model.CaseData.Case.CaseSheets[i].Id hide">
<form method="post">
<input type="hidden" asp-for="CaseData.Case.Id" />
<input type="hidden" asp-for="CaseData.Case.CaseSheets[i].Id" />
<input type="hidden" asp-for="CaseData.CaseSheetNum" />
<h2>@Model.CaseData.Case.CaseSheets[i].Title</h2>
@for (var n = 0; n < Model.CaseData.Case.CaseSheets[i].CaseSheetOptions.Count; n++)
{
Model.CaseData.CaseSheetOptionNum = n;
await Html.RenderPartialAsync("PartialName", Model.CaseData);
}
<br /><p class="text-right"><button type="submit" name="Submit" class="btn">Save sheet</button></p>
</form>
</div>
}
Когда я публикую формы, я могу получить это значение CaseData.Case.Id
, но CaseData.Case.CaseSheets
пусто (должно бытьСписок из 6 пунктов).
Модели:
public class Case
{
public Guid Id { get; set; }
public Guid AccountId { get; set; }
public DateTime CreatedDate { get; set; }
public Guid CreatedBy { get; set; }
public DateTime? ClosedDate { get; set; }
public Guid? ClosedBy { get; set; }
public int Status { get; set; }
public IList<CaseSheet> CaseSheets { get; set; }
}
public class CaseSheet
{
public Guid Id { get; set; }
public string Title { get; set; }
public int MajorNum { get; set; }
public int MinorNum { get; set; }
public Guid CaseId { get; set; }
public int Status { get; set; }
public IList<CaseSheetOption> CaseSheetOptions { get; set; }
}
public class CaseSheetOption
{
public Guid Id { get; set; }
public string Headline { get; set; }
public string Text { get; set; }
public bool Yes { get; set; }
public bool No { get; set; }
public string Comment { get; set; }
public Guid CaseSheetId { get; set; }
public int Priority { get; set; }
public int Status { get; set; }
}
public class CaseData
{
public Case Case { get; set; }
public int CaseSheetNum { get; set; }
public int CaseSheetOptionNum { get; set; }
}
Контроллер:
[BindProperty]
public CaseData CaseData { get; set; }
public async Task<IActionResult> OnGetAsync(Guid id)
{
var c = await _dbContext.Cases.Include(x => x.CaseSheets).ThenInclude(x => x.CaseSheetOptions).FirstOrDefaultAsync(x => x.Id == id);
c.CaseSheets = c.CaseSheets.OrderBy(x => x.MajorNum).ThenBy(x => x.MinorNum).ToList();
foreach (var sheet in c.CaseSheets)
{
sheet.CaseSheetOptions = sheet.CaseSheetOptions.OrderBy(x => x.Priority).ToList();
}
CaseData = new CaseData
{
Case = c,
CaseSheetNum = 0,
CaseSheetOptionNum = 0
};
return Page();
}
public async Task<IActionResult> OnPostAsync()
{
var caseSheets = CaseData.Case.CaseSheets;
int caseSheetNum = CaseData.CaseSheetNum;
Guid caseSheetId = caseSheets[caseSheetNum].Id;
var sheetOptions = await _dbContext.CaseSheetOptions.Where(x => x.CaseSheetId == caseSheetId && x.Status == 1).ToListAsync();
foreach (var option in caseSheets[caseSheetNum].CaseSheetOptions)
{
var sheetOption = sheetOptions.FirstOrDefault(x => x.Id == option.Id);
sheetOption.Yes = option.Yes;
sheetOption.No = option.No;
sheetOption.Comment = option.Comment;
_dbContext.Update(sheetOption);
}
await _dbContext.SaveChangesAsync();
return RedirectToPage("", new { caseSheetId });
}
В OnPost()
caseSheets.GetType()
составляет Generic.List
,но caseSheets.Count
равно 0 (должно быть 6).
Поля ввода имеют имена, например CaseData.Case.CaseSheets[2].CaseSheetOptions[0].Id
.
Чего мне не хватает?