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

asp.net-core-mvc drop-down-menu entity-framework-core one-to-many

Question

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
-1
4/24/2019 5:00:50 PM

Accepted Answer

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

1
4/25/2019 7:07:15 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow