如何在 ASP.NET Core MVC 中的 Edit 方法的下拉字段中显示 selectedValue(来自数据库)
How can I display the selectedValue (from database) in a dropdown-field from the Edit method in ASP.NET Core MVC
在 ASP.NET Core 3.1 MVC 项目的以下代码中,Get
操作方法 Edit (...)
显示邮政编码的下拉列表。似乎默认情况下,下拉列表将列表中的第一个邮政编码(即 1301)显示为所选值。
如果我想在下拉列表中显示从数据库中选择的值,如何在不创建视图模型的情况下通过操作方法 Edit(...)
实现它?
我有一个相当复杂的数据模型,其中邮政编码和城市是 table 商店中的成员,但是 table 通过连词 [=] 与产品具有多对多关系41=] 产品商店。我是初学者,所以我一直在尝试按照本教程进行操作,但到目前为止我还没有做到。 https://docs.microsoft.com/en-us/aspnet/core/data/ef-mvc/update-related-data?view=aspnetcore-5.0
这是我在控制器中的 Edit
get 方法:
// GET: Products/Edit/5
public async Task<IActionResult> Edit(int? id) {
if (id == null) {
return NotFound();
}
var product = await _context.Product
.Include(p => p.Brand)
.Include(p => p.Price)
.Include(p => p.ProductCategory)
.ThenInclude(p => p.Category)
.Include(p => p.ProductShop)
.ThenInclude(ps => ps.Shop)
.Include(s => s.ProductShop)
.ThenInclude(ps => ps.Shop)
.ThenInclude(s => s.Zip)
.FirstOrDefaultAsync(m => m.Id == id);
if (product == null) {
return NotFound();
}
ViewData["BrandId"] = new SelectList(_context.Brand, "Id", "BrandName", product.BrandId);
// this is the method where I want to display zipcodes with the help from viewbag
ViewData["ZipId"] = new SelectList(_context.Zipcode.ToList(), "Id", "Zip", product.ProductShop.FirstOrDefault().Shop.Zip.Id);
PopulateSelectedCategories(product);
return View(product);
}
我在视图中的下拉列表:
@model Vegetarian_Products.Models.DB.Product
@*@model Vegetarian_Products.ViewModels.ProductDetailsVM*@
@{
ViewData["Title"] = "Edit";
}
<h1>Edit</h1>
<h4>Product</h4>
<hr />
<div class="row">
<div class="col-md-4">
<form asp-action="Edit">
//....
<div class="form-group">
<label asp-for="ProductShop.FirstOrDefault().Shop.Zip.Zip" class="control-label"></label>
<select asp-for="ProductShop.FirstOrDefault().Shop.Zip.Zip" class="form-control" asp-items="ViewBag.ZipId">
</select>
<span asp-validation-for="ProductShop.FirstOrDefault().Shop.Zip.Zip" class="text-danger" />
</div>
<div class="form-group">
<input type="submit" value="Save" class="btn btn-primary" />
</div>
</form>
</div>
</div>
<div>
<a class="btn btn-primary" asp-controller="Products" asp-action="Index">Back to List</a>
</div>
@section Scripts {
@{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}
邮政编码型号 class:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
namespace Vegetarian_Products.Models.DB {
public partial class Zipcode {
public Zipcode() {
Shop = new HashSet<Shop>();
}
public int Id { get; set; }
[Required(ErrorMessage = "Tilføj postnr")]
[DisplayName("Postnr")]
public int Zip { get; set; }
[DisplayName("By")]
public string City { get; set; }
public virtual ICollection<Shop> Shop { get; set; }
}
}
编辑视图的屏幕截图 运行,并且没有从数据库中选择值:
table-关系模型:
您可以尝试像这样创建 SelectList
var selectedId = product.ProductShop.FirstOrDefault()?.Shop.Zip.Id;
ViewBag.ZipId = await _context.Zipcode
.Select(w => new SelectListItem
{
Value = w.Id.ToString(),
Text = w.Zip.ToString(),
Selected = w.Id == selectedId
})
.ToListAsync();
只有当 Product 和 Shop 以 1 对 1 的关系直接相关或如果 Product 与 Shop 以 N 对 1 的关系相关时,在这种情况下具有 selected 值才有意义。
由于一个产品可以与许多商店相关,而一个商店又可以与许多产品相关,因此在任一视图中使用具有 selected 值的 select 字段是没有意义的.
如果我对这个模型的理解是正确的,那么您想要显示它的方式是在“产品编辑”页面中有一个商店列表,在“商店编辑”页面中有一个产品列表。
所以我建议您将此列表分成一个选定的列表视图数据和一个未select编辑的列表视图数据,而不是将它们捆绑在一起,然后使用一种添加和删除项目的方法,这些项目将在视觉上将它们从一个列表到另一个列表。
在 ASP.NET Core 3.1 MVC 项目的以下代码中,Get
操作方法 Edit (...)
显示邮政编码的下拉列表。似乎默认情况下,下拉列表将列表中的第一个邮政编码(即 1301)显示为所选值。
如果我想在下拉列表中显示从数据库中选择的值,如何在不创建视图模型的情况下通过操作方法 Edit(...)
实现它?
我有一个相当复杂的数据模型,其中邮政编码和城市是 table 商店中的成员,但是 table 通过连词 [=] 与产品具有多对多关系41=] 产品商店。我是初学者,所以我一直在尝试按照本教程进行操作,但到目前为止我还没有做到。 https://docs.microsoft.com/en-us/aspnet/core/data/ef-mvc/update-related-data?view=aspnetcore-5.0
这是我在控制器中的 Edit
get 方法:
// GET: Products/Edit/5
public async Task<IActionResult> Edit(int? id) {
if (id == null) {
return NotFound();
}
var product = await _context.Product
.Include(p => p.Brand)
.Include(p => p.Price)
.Include(p => p.ProductCategory)
.ThenInclude(p => p.Category)
.Include(p => p.ProductShop)
.ThenInclude(ps => ps.Shop)
.Include(s => s.ProductShop)
.ThenInclude(ps => ps.Shop)
.ThenInclude(s => s.Zip)
.FirstOrDefaultAsync(m => m.Id == id);
if (product == null) {
return NotFound();
}
ViewData["BrandId"] = new SelectList(_context.Brand, "Id", "BrandName", product.BrandId);
// this is the method where I want to display zipcodes with the help from viewbag
ViewData["ZipId"] = new SelectList(_context.Zipcode.ToList(), "Id", "Zip", product.ProductShop.FirstOrDefault().Shop.Zip.Id);
PopulateSelectedCategories(product);
return View(product);
}
我在视图中的下拉列表:
@model Vegetarian_Products.Models.DB.Product
@*@model Vegetarian_Products.ViewModels.ProductDetailsVM*@
@{
ViewData["Title"] = "Edit";
}
<h1>Edit</h1>
<h4>Product</h4>
<hr />
<div class="row">
<div class="col-md-4">
<form asp-action="Edit">
//....
<div class="form-group">
<label asp-for="ProductShop.FirstOrDefault().Shop.Zip.Zip" class="control-label"></label>
<select asp-for="ProductShop.FirstOrDefault().Shop.Zip.Zip" class="form-control" asp-items="ViewBag.ZipId">
</select>
<span asp-validation-for="ProductShop.FirstOrDefault().Shop.Zip.Zip" class="text-danger" />
</div>
<div class="form-group">
<input type="submit" value="Save" class="btn btn-primary" />
</div>
</form>
</div>
</div>
<div>
<a class="btn btn-primary" asp-controller="Products" asp-action="Index">Back to List</a>
</div>
@section Scripts {
@{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}
邮政编码型号 class:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
namespace Vegetarian_Products.Models.DB {
public partial class Zipcode {
public Zipcode() {
Shop = new HashSet<Shop>();
}
public int Id { get; set; }
[Required(ErrorMessage = "Tilføj postnr")]
[DisplayName("Postnr")]
public int Zip { get; set; }
[DisplayName("By")]
public string City { get; set; }
public virtual ICollection<Shop> Shop { get; set; }
}
}
编辑视图的屏幕截图 运行,并且没有从数据库中选择值:
table-关系模型:
您可以尝试像这样创建 SelectList
var selectedId = product.ProductShop.FirstOrDefault()?.Shop.Zip.Id;
ViewBag.ZipId = await _context.Zipcode
.Select(w => new SelectListItem
{
Value = w.Id.ToString(),
Text = w.Zip.ToString(),
Selected = w.Id == selectedId
})
.ToListAsync();
只有当 Product 和 Shop 以 1 对 1 的关系直接相关或如果 Product 与 Shop 以 N 对 1 的关系相关时,在这种情况下具有 selected 值才有意义。
由于一个产品可以与许多商店相关,而一个商店又可以与许多产品相关,因此在任一视图中使用具有 selected 值的 select 字段是没有意义的.
如果我对这个模型的理解是正确的,那么您想要显示它的方式是在“产品编辑”页面中有一个商店列表,在“商店编辑”页面中有一个产品列表。
所以我建议您将此列表分成一个选定的列表视图数据和一个未select编辑的列表视图数据,而不是将它们捆绑在一起,然后使用一种添加和删除项目的方法,这些项目将在视觉上将它们从一个列表到另一个列表。