How to Create Drop-Down-List from Database in (Asp.net Core - MVC)?

我只是一个虾纸丫 提交于 2020-12-15 04:38:04

问题


I have 2 models:

  1. News Model
  2. TypeOfNew Model

with (one to many relationship), every (TypeOfNew) has one or multiple news.

News Model:

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 Model:

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

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

in Create View(News), I want to display drop-down-list of (TypeOfNew). so when I post (Create View) I want to store (TypeId) of (TypeOfNew model) in (NewsTypeId) of (News model).

So, what should I do in:

  1. create Action (Get).
  2. create Action (Post).
  3. Create View.

回答1:


Assume that you have set below tables in dbcontext:

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

You could refer to below steps to achieve your requirements:

1.Create action GET

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

2.Create action 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.Create View

@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>

For configuring one-to-many relationships,you could refer to

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



来源:https://stackoverflow.com/questions/55832888/how-to-create-drop-down-list-from-database-in-asp-net-core-mvc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!