Tôi đang cố gắng sử dụng Trình trợ giúp thẻ MVC6 để tạo danh sách thả xuống của CountryCode và CountryName để người dùng có thể chọn quốc gia của họ sau khi đăng ký. Phần liên quan của quan điểm trông như thế này cho đến nayDanh sách thả xuống MVC6 của các quốc gia
<form asp-controller="Manage" asp-action="EditCountry" asp-route-returnurl="@ViewData["ReturnUrl"]">
<div asp-validation-summary="ValidationSummary.ModelOnly" class="text-danger"></div>
<select asp-for="CountryCode" asp-items="@Model.Countries"></select>
phần liên quan của viewmodel trông như thế này
[Display(Name = "Country")]
public string CountryCode { get; set; }
public IEnumerable<Country> Countries { get; set; }
A Country trông như thế này
public partial class Country
{
[Key]
public string CountryCode { get; set; }
public string CountryName { get; set; }
public virtual ICollection<ApplicationUser> Users { get; set; }
}
Bộ điều khiển trả về một danh sách các quốc gia cho chế độ xem mô hình
var model = new IndexViewModel
{
CountryCode = user.CountryCode,
Countries =_customersContext.Countries.OrderBy(c=>c.CountryName),
};
return View(model);
}
nhưng trong chế độ xem asp-items="@Model.Countries"
có hình vuông Cannot convert Country to SelectListItem
Ngoài ra tôi không thể tìm thấy cách thức để chỉ định CountryCode làm thuộc tính trả về và CountryName làm tài sản hiển thị.
Cảm ơn bạn @Vince giải thích rõ ràng. Điều đó làm việc một điều trị. – Vague