css ASP.NET Core 6 MVC:创建idea时,显示数据库表中类别的Selectbox返回null

i7uaboj4  于 2023-05-30  发布在  .NET
关注(0)|答案(1)|浏览(118)

我有一个选择框,它使用存储库检索所有类别,但当创建一个想法时,所选类别返回null。
这些是我的模型类:

public class Category
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
}

public class Idea
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public string Rating { get; set; }
    public string Status { get; set; }
    public DateTime Date { get; set; }

    [ForeignKey("Category")]
    public int CategoryId { get; set; }
    public Category? Category { get; set; }
    public  IEnumerable<Category>? Categories { get; set; }
}

这是当我必须创建一个想法时的主控制器,只要我单击创建图标,选择框就会检索所有类别,获取类别ID并将其传递给CategoryId(想法模型中的外键),但它无法将其链接到类别表中的实际ID,因此当显示像@model Idea.Category.Name这样的项目时,它返回null

[HttpPost
public async Task<IActionResult> AddIdea(Idea viewModel)
{
    if (ModelState.IsValid)
    {
        var idea = new Idea
                       {
                           Name = viewModel.Name,
                           Description = viewModel.Description,
                           Rating = viewModel.Rating,
                           Status = viewModel.Status,
                           Date = viewModel.Date,
                           CategoryId = viewModel.CategoryId, 
                           Category = viewModel.Category,  // this is where it says null, without it the modelState becomes false.
                       };

        _ideaRepository.Add(idea);

        // Redirect to home or any other page
        return RedirectToAction("Index"); 
    }

    var categories = await _categoryRepository.GetAllCategories();
    ViewBag.Categories = new SelectList(categories, "Id", "Name");

    return View(viewModel);
}
nnsrf1az

nnsrf1az1#

像下面这样修改你的代码,这个问题可以解决。你应该先从数据库中获取category,因为在添加新模型页面中,类别为空。我看到你有categoryId,所以你可以得到category,所以请使用我的代码来解决这个问题。

[HttpPost]
public async Task<IActionResult> AddIdea(Idea viewModel)
{
    if (ModelState.IsValid)
    {
        // Get category and use it
        var category = await _categoryRepository.GetCategoryById(viewModel.CategoryId);

        var idea = new Idea
                       {
                           Name = viewModel.Name,
                           Description = viewModel.Description,
                           Rating = viewModel.Rating,
                           Status = viewModel.Status,
                           Date = viewModel.Date,
                           CategoryId = viewModel.CategoryId, 
                           //Category = viewModel.Category,  // this is where it says null, without it the modelState becomes false.
                           // Use category, it will not null
                           Category = category;
                       };

        _ideaRepository.Add(idea);

        // Redirect to home or any other page
        return RedirectToAction("Index"); 
    }

    var categories = await _categoryRepository.GetAllCategories();
    ViewBag.Categories = new SelectList(categories, "Id", "Name");

    return View(viewModel);
}

相关问题