我正试图用数据库中的信息填充剃刀页面上的下拉列表(我的网站用于文档上传/下载(。当他们上传文件时,它会询问供应商Id,因为这是将我的"文件"one_answers"供应商(用户("链接在一起的外键。
在下拉列表中,我希望他们能够选择供应商名称,但在文件db-vendorId中输入。
我可以用以下代码手动填充它:
<select asp-for="Files.VendorId">
<option value="3950">Girvan Early Growers Ltd</option>
</select>
但在某一点上,我们可能有多达50家供应商,所以这并不理想。
下面我将包括我的页面模型和我的cshtml页面,看看它是否有帮助。
页面模型:
using FarmersPortal.Data;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion.Internal;
namespace FarmersPortal.Pages
{
[Authorize(Roles ="Admin")]
public class CreateModel : PageModel
{
private readonly FarmersPortal.Data.filedbContext _context;
public CreateModel(FarmersPortal.Data.filedbContext context)
{
_context = context;
}
public IQueryable<Data.Vendor> VendorList { get; set; }
[BindProperty]
public List<Data.Vendor> Vendores { get; set; }
public void OnGet()
{
Vendores = _context.Vendors.ToList();
}
[BindProperty]
public Data.File Files { get; set; }
public Data.Vendor Vendors { get; set; }
// To protect from overposting attacks, enable the specific properties you want to bind to, for
// more details, see https://aka.ms/RazorPagesCRUD.
public async Task<IActionResult> OnPostAsync()
{
if (!ModelState.IsValid)
{
return Page();
}
_context.Files.Add(Files);
await _context.SaveChangesAsync();
return RedirectToPage("./Index");
}
}
}
.cshtml:
@page
@model FarmersPortal.Pages.CreateModel
@{
ViewData["Title"] = "Create";
}
<style>
body {
background-image: url('hero-range-1.jpg');
height: 100%;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
}
</style>
<h1 style="color: white">Create</h1>
<h4 style ="color: white">Files</h4>
<hr/>
<div class="row">
<div class="col-md-4">
<form method="post">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="Files.Number" class="control-label" style="color: white"></label>
<input asp-for="Files.Number" class="form-control" />
<span asp-validation-for="Files.Number" class="text-danger"></span>
</div>
<div class="form-group">
<div class="form-group">
<label asp-for="Files.FileType" class="control-label" style="color: white"></label>
<input asp-for="Files.FileType" class="form-control" />
<span asp-validation-for="Files.FileType" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Files.VendorId" class="control-label" style="color: white"></label>
<input asp-for="Files.VendorId" class="form-control" />
<span asp-validation-for="Files.VendorId" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Files.Haulier" class="control-label" style="color: white"></label>
<input asp-for="Files.Haulier" class="form-control" />
<span asp-validation-for="Files.Haulier" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Files.Comments" class="control-label" style="color: white"></label>
<input asp-for="Files.Comments" class="form-control" />
<span asp-validation-for="Files.Comments" class="text-danger"></span>
</div>
<select asp-for="Files.VendorId">
<option value="3950">Girvan Early Growers Ltd</option>
</select>
<input type="submit" value="Create" class="btn btn-primary" />
</div>
</form>
</div>
</div>
<div>
<a asp-page="Index">Back to List</a>
</div>
@section Scripts {
@{
await Html.RenderPartialAsync("_ValidationScriptsPartial");
}
}
我该怎么做?
根据您的描述,在Vendor
表中,有Id
、Name
等属性,所以如果您想让下拉列表显示"Name"为文本,"Id"为值,您可以参考以下代码:
public void OnGet()
{
List<SelectListItem> test = new List<SelectListItem>();
Vendores = _context.Vendors.ToList();
foreach (var item in Vendores)
{
test.Add(new SelectListItem {Text=item.Name,Value = item.Id.ToString() });
}
ViewData["demo"] = test;
}
在视图中
<select asp-for="Files.VendorId" asp-items="@ViewData["demo"]"></select>
然后dropdownlist
将显示名称但传递id。
您可以参考此链接了解更多信息。