如何在(Asp.net Core - MVC)中从数据库创建下拉列表?

问题描述 投票:-1回答:1

我有2个型号:

  1. 新闻模型
  2. TypeOfNew模型

与(一对多关系),每个(TypeOfNew)有一个或多个新闻。

新闻型号:

public class News
{
    [Required(ErrorMessage = "ID إجباري.")]
    [Key]
    public int ID { get; set; }

    [Required(ErrorMessage = "الحقل إجباري.")]
    [Display(Name = "عنوان الخبر")]
    public string Title { get; set; }

    [Required(ErrorMessage = "الحقل إجباري.")]
    [Display(Name = "مصدر الخبر")]
    public string Source { get; set; }

    [Required(ErrorMessage = "الحقل إجباري.")]
    [Display(Name = "الوصف")]

    [MaxLength(5000)]
    public string Description { set; get; }

    [Display(Name = "نوع الخبر")]
    public int NewsTypeId { set; get; }
    public TypeOfNew TypeOfNew { set; get; }
}

TypeOfNew型号:

public class TypeOfNew
{
    [Key]
    public int TypeId { set; get; }
    [Display(Name = " نوع الخبر")]

    public string TypeName { set; get; }
    public ICollection<News> News { get; set; }
}

在创建视图(新闻)中,我想显示(TypeOfNew)的下拉列表。因此,当我发布(创建视图)时,我想在(新闻模型)的(新闻类型)中存储(TypeOf新模型)(TypeId)。

那么,我该怎么做:

  1. 创建动作(获取)。
  2. 创建动作(发布)。
  3. 创建视图。
drop-down-menu asp.net-core-mvc entity-framework-core one-to-many
1个回答
1
投票

假设您在dbcontext中设置了以下表:

public DbSet<News> News { get; set; }
public DbSet<TypeOfNew> TypeOfNews { get; set; }`

您可以参考以下步骤来实现您的要求:

1.创造行动GET

public IActionResult Create()
    {
        ViewData["NewsTypeId"] = new SelectList(_context.TypeOfNews, "TypeId", "TypeName");
        return View();
    }

2.创造行动POST

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("ID,Title,Source,Description,NewsTypeId")] News news)
    {
        if (ModelState.IsValid)
        {
            _context.Add(news);
            await _context.SaveChangesAsync();
            return RedirectToAction(nameof(Index));
        }
        ViewData["NewsTypeId"] = new SelectList(_context.TypeOfNews, "TypeId", "TypeName", news.NewsTypeId);
        return View(news);
    }

3.创建视图

@model News

<h1>Create</h1>
<hr />
<div class="row">
<div class="col-md-4">
    <form asp-action="Create">
        <div asp-validation-summary="ModelOnly" class="text-danger"></div>
        <div class="form-group">
            <label asp-for="Title" class="control-label"></label>
            <input asp-for="Title" class="form-control" />
            <span asp-validation-for="Title" class="text-danger"></span>
        </div>
        <div class="form-group">
            <label asp-for="Source" class="control-label"></label>
            <input asp-for="Source" class="form-control" />
            <span asp-validation-for="Source" class="text-danger"></span>
        </div>
        <div class="form-group">
            <label asp-for="Description" class="control-label"></label>
            <input asp-for="Description" class="form-control" />
            <span asp-validation-for="Description" class="text-danger"></span>
        </div>
        <div class="form-group">
            <label asp-for="NewsTypeId" class="control-label"></label>
            <select asp-for="NewsTypeId" class="form-control" asp-items="@ViewBag.NewsTypeId"></select>
            <span asp-validation-for="NewsTypeId" class="text-danger"></span>
        </div>
        <div class="form-group">
            <input type="submit" value="Create" class="btn btn-primary" />
        </div>
    </form>
</div>

有关配置一对多关系的信息,请参阅

https://docs.microsoft.com/en-us/ef/core/modeling/relationships#definition-of-terms

© www.soinside.com 2019 - 2024. All rights reserved.