问题描述
我有2个型号:
- 新闻模型
- TypeOfNew模型
(一对多的关系),每个(TypeOfNew)都有一个或多个新闻.
with (one to many relationship), every (TypeOfNew) has one or multiple news.
新闻模型:
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模型:
TypeOfNew Model:
public class TypeOfNew
{
[Key]
public int TypeId { set; get; }
[Display(Name = " نوع الخبر")]
public string TypeName { set; get; }
public ICollection<News> News { get; set; }
}
在创建视图(新闻)"中,我要显示(TypeOfNew)的下拉列表.所以当我发布(创建视图)时,我想将(TypeOfNew模型)的(TypeId)存储在(新闻模型)的(NewsTypeId)中.
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).
那么,我该怎么做:
- 创建操作(获取).
- 创建动作(发布).
- 创建视图.
推荐答案
假定您已在dbcontext中设置了以下表格:
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.创建动作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>
有关配置一对多关系,可以参考
For configuring one-to-many relationships,you could refer to
https://docs.microsoft .com/zh-CN/ef/core/modeling/relationships#definition-of-terms
这篇关于如何在(Asp.net Core-MVC)中从数据库创建下拉列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!