2012-05-29 26 views
5

Tôi đã xem qua các câu hỏi và câu trả lời khác trên trang web này nhưng không thể tìm thấy câu trả lời tôi cần.Làm thế nào để vượt qua một mô hình có chứa một mô hình IEnumerable (phức tạp) vào một bộ điều khiển từ một khung nhìn C# MVC3?

Tôi có một thực thể StudentRecord:

public class StudentRecord : Persistent { 
     public virtual string LastName { get; set; } 
     public virtual string FirstName { get; set; } 
     public virtual DateTime Dob { get; set; } 
     public virtual int StudentRef { get; set; } 
     public virtual IEnumerable<StudentAddress> Addresses { get; set; } 
     public virtual StudentAddress Address { get; set; } 
     public virtual string Notes { get; set; } 
    } 

Như bạn có thể nhìn thấy nó chứa một thực thể StudentAddress duy nhất và cũng là một IEnumerable của StudentAddress:

public class StudentAddress: Persistent { 
     public virtual int StudentRef { get; set; } 
     public virtual string Addressee { get; set; } 
     public virtual string Property { get; set; } 
     public virtual string District { get; set; } 
     public virtual string PostalTown { get; set; } 
     public virtual string County { get; set; } 
     public virtual string Postcode { get; set; } 
    } 

tôi đi qua một hồ sơ học sinh đến một cái nhìn , chứa trong chế độ xem:

public class UserViewModel { 
     public StudentRecord Student;  
     public ICurrentUserService CurrentUserService; 
     public ParentUser ParentUser;   
    } 

Sau đó, hiển thị nó dưới dạng để tôi t có thể được chỉnh sửa và gửi biểu mẫu chuyển StudentRecord trở lại bộ điều khiển. Tất cả các công trình tốt ngoại trừ các địa chỉ trong StudentRecord là null. StudentAddress duy nhất trong StudentRecord là nếu một địa chỉ mới được thêm vào, và nó cũng hoạt động tốt.

Có thể chỉnh sửa và gửi các địa chỉ trở lại với bộ điều khiển, hay tôi cần phải có chúng trong một hình thức riêng biệt trên một trang riêng biệt? Tôi có thể làm điều đó nhưng muốn có tất cả trong một.

Vấn đề của tôi có thể là nó không phải là có thể, hoặc nó có thể là cách tôi đặt địa chỉ vào biểu mẫu. Một học sinh có thể có nhiều hơn một địa chỉ.

Dưới đây là hình thức: (Tôi đã tước ra một số layout html cho rõ ràng 'Thêm một địa chỉ' tickbox cho thấy phần Địa chỉ New Student với jquery..)

@using (Html.BeginForm()) { 
    Personal Details 
    Full Name: @Html.TextBoxFor(x => x.Student.FirstName) @Html.TextBoxFor(x => x.Student.LastName) 
    DOB: @Html.TextBoxFor(x => x.Student.Dob) 

    @if (Model.Student.Addresses.Any()) { 
     // Only print addresses if they exist 
      int count = 1; 
      int element = 0; 
       @if (Model.Student.Addresses.Count() > 1) { 
        foreach (var address in Model.Student.Addresses) { 
         Student Address @count 
         Addressee @Html.TextBoxFor(x => x.Student.Addresses.ElementAt(element).Addressee) 
         Property @Html.TextBoxFor(x => x.Student.Addresses.ElementAt(element).Property) 
         District @Html.TextBoxFor(x => x.Student.Addresses.ElementAt(element).District) 
         Postal Town @Html.TextBoxFor(x => x.Student.Addresses.ElementAt(element).PostalTown) 
         County @Html.TextBoxFor(x => x.Student.Addresses.ElementAt(element).County) 
         Postcode @Html.TextBoxFor(x => x.Student.Addresses.ElementAt(element).Postcode) 
         count++; 
         element++; 
        } //end foreach 
       } else { 
        Student Address 
        Addressee @Html.TextBoxFor(x => x.Student.Addresses.ElementAt(0).Addressee) 
        Property @Html.TextBoxFor(x => x.Student.Addresses.ElementAt(0).Property) 
        District @Html.TextBoxFor(x => x.Student.Addresses.ElementAt(0).District) 
        Postal Town @Html.TextBoxFor(x => x.Student.Addresses.ElementAt(0).PostalTown) 
        County @Html.TextBoxFor(x => x.Student.Addresses.ElementAt(0).County) 
        Postcode @Html.TextBoxFor(x => x.Student.Addresses.ElementAt(0).Postcode) 
       } @*end if (Model.Student.Addresses.Count() > 1)*@ 

       Add another address @Html.CheckBox("Add another address", false, new {@id = "newBox"}) 

       New Student Address 
       Addressee @Html.TextBoxFor(x => x.Student.Address.Addressee) 
       Property @Html.TextBoxFor(x => x.Student.Address.Property) 
       District @Html.TextBoxFor(x => x.Student.Address.District) 
       Postal Town @Html.TextBoxFor(x => x.Student.Address.PostalTown) 
       County @Html.TextBoxFor(x => x.Student.Address.County) 
       Postcode @Html.TextBoxFor(x => x.Student.Address.Postcode) 
    } else { 
     No address for this student. 
    } @*end if (Model.Student.Addresses.Any())*@ 

    Notes: @Html.TextAreaFor(x => x.Student.Notes, new { @style = "width: 100%;"}) 

    <input type="submit" value="Send" class="btn btn-primary" style="clear: both;"/> 
} @*end of form*@ 

Trả lời

8

Vấn đề là các name các thuộc tính của các điều khiển nhập văn bản không chứa các giá trị chính xác. Tôi mời bạn đọc following blog post để hiểu rõ hơn quy ước được sử dụng bởi trình kết nối mô hình mặc định để liên kết với các bộ sưu tập và từ điển.

Sau đó, tôi sẽ khuyên bạn nên sử dụng các mẫu biên tập thay vì viết vòng lặp foreach trong quan điểm của bạn:

@using (Html.BeginForm()) { 
    Personal Details 

    @Html.LabelFor(x => x.Student.FirstName, "Full Name:") 
    @Html.EditorFor(x => x.Student.FirstName) 
    @Html.EditorFor(x => x.Student.LastName) 

    @Html.LabelFor(x => x.Student.Dob, "DOB:") 
    @Html.TextBoxFor(x => x.Student.Dob) 

    @if (Model.Student.Addresses.Any()) { 
     @Html.EditorFor(x => x.Student.Addresses) 
    } else { 
     <text>No address for this student.</text> 
    } 

    @Html.LabelFor(x => x.Student.Notes, "Notes:") 
    @Html.TextAreaFor(x => x.Student.Notes, new { @style = "width: 100%;"}) 

    <input type="submit" value="Send" class="btn btn-primary" style="clear: both;"/> 
} 

và sau đó xác định một mẫu biên tập tùy chỉnh sẽ được tự động trả lại cho mỗi phần tử của bộ sưu tập Địa chỉ (~/Views/Shared/EditorTemplates/StudentAddress.cshtml) :

@model StudentAddress 
@Html.LabelFor(x => x.Addressee, "Addressee") 
@Html.EditorFor(x => x.Addressee) 

@Html.LabelFor(x => x.Property, "Property") 
@Html.EditorFor(x => x.Property) 

@Html.LabelFor(x => x.District, "District") 
@Html.EditorFor(x => x.District) 

@Html.LabelFor(x => x.PostalTown, "Postal Town") 
@Html.EditorFor(x => x.PostalTown) 

@Html.LabelFor(x => x.County, "County") 
@Html.EditorFor(x => x.County) 

@Html.LabelFor(x => x.Postcode, "Postcode") 
@Html.EditorFor(x => x.Postcode) 

Nhưng tất cả điều này là tĩnh. Nếu bạn muốn để có thể tự động thêm và loại bỏ các địa chỉ tôi mời bạn đọc following blog post từ Steven Sanderson, trong đó ông mô tả làm thế nào một helper HTML tùy chỉnh có thể được sử dụng để tạo tên riêng cho các lĩnh vực đầu vào (Html.BeginCollectionItem) và sử dụng AJAX để thêm hàng mới.

+0

Cảm ơn bạn đã phản hồi nhanh chóng. Tôi sẽ cố gắng và xem xét vấn đề đọc được đề xuất của bạn. Tôi sẽ đăng kết quả của mình ở đây. Cảm ơn nhiều. – tekiegirl

+0

Cảm ơn bạn, điều này đã làm việc hoàn hảo. Tôi cũng sẽ xem xét tự động thêm các địa chỉ. – tekiegirl

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