2016-01-12 14 views
8

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ị.

Trả lời

35

Cách tôi thực hiện thả xuống của tôi hơi giống ngoại trừ trong ViewModel của tôi, thuộc tính của tôi thuộc loại SelectList thay vì IEnumerable<>.

public class HomeViewModel 
{ 
    public string CountryCode { get; set; } 
    public SelectList CountryList { get; set; } 
} 

Sau đó, trong bộ điều khiển, tôi lấy dữ liệu và chuyển đổi thành danh sách ẩn danh với hai thuộc tính “Id” và “Value”.

Lần lượt, tôi tạo mới SelectList() chuyển vào danh sách ẩn danh chỉ định dataValueField là gì và dataTextField là gì.

public IActionResult Index() 
{ 
    var countries = _customersContext.Countries.OrderBy(c => c.CountryName).Select(x => new { Id = x.Code, Value = x.Name }); 

    var model = new HomeViewModel(); 
    model.CountryList = new SelectList(countries, "Id", "Value"); 

    return View(model); 
} 

Cuối cùng, trong Xem:

<div class="form-group"> 
    <label asp-for="CountryCode" class="col-md-2 control-label"></label> 
    <div class="col-md-10"> 
     <select asp-for="CountryCode" asp-items="@Model.CountryList"></select> 
    </div> 
</div> 
+0

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

2
public class MyViewModel //MODEL LAYER 
    { 
     public int CountryId { get; set; } 

    public string CountryName { get; set; } 
    public List<Employee> EmployeesList { get; set; } 
} 

public class Employee 
{ 
    public int Id { get; set; } 
    public string FullName { get; set; } 
} 

public IActionResult Contact1() //CONTROLLER 
     { 

     MyViewModel N1 = new MyViewModel(); 
     List<Employee> N2 = new List<Employee>() 
     { 
      new Employee { Id=1,FullName="sivaragu" }, 
      new Employee { Id=2,FullName="siva" }, 
        new Employee { Id=3,FullName="SENTHIL" } 
     }; 

     ViewBag.MovieType = N2; 
     return View();    
    } 

CSHTML (MVC6)

<select asp-for="CountryId" asp-items="@(new SelectList(@ViewBag.MovieType,"Id","FullName"))">  
    </select> 
+0

+0

MÃ CSHTML NÀY –

1

tôi đã đề xuất một cách khác để làm điều này bằng cách mở rộng SelectTagHelper với một số chi tiết các thuộc tính có thể làm cho kiểu phát triển này thuận tiện hơn. Vấn đề được thảo luận here.

Nó dựa trên một lớp SelectListDescriptor chứa danh sách các mục, thuộc tính để hiển thị trường văn bản và giá trị tương ứng. Sau đó, trong chế độ xem, một loại đơn giản là

<select asp-descriptor="@ViewBag.CountryDescriptor"><option value="">-- Choose country</option<</select>.

Bộ mô tả quốc gia chỉ là new SelectListDescriptor(nameof(Country.CountryCode), nameof(Country.CountryName), countries). Điều này tránh các chuỗi ma thuật bằng cách sử dụng sức mạnh của `tên nhà điều hành '

Các vấn đề liên quan