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*@
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
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