2011-01-18 34 views
29

Vấn đề này đã khiến tôi lo lắng trong hai ngày nay. Có một số bài viết tương tự, nhưng không có bài nào giải quyết vấn đề của tôi hoàn toàn.Mô hình chứa danh sách các mô hình (MVC-3, Dao cạo)

Sử dụng MVC-3, Razor cú pháp:

- EDIT.cshtml -

@using (Html.BeginForm("Edit", "My", FormMethod.Post, new { enctype = "multipart/form-data" })) 
{ 
    <!-- Some fields... --> 
    <div class="editor-field"> 
     @Html.TextAreaFor(m => m.LongDescription) 
     @Html.ValidationMessageFor(m => m.LongDescription) 
    </div> 

    <!-- Some more fields work... Including picture upload (summary).--> 
    <input name="button" type="submit" value="Add Picture" /> 

    <!-- Picture Item display --> 
    @foreach(var thumbnail in Model.ThumbnailImagePathAndNames) 
    { 
     <img src="@Url.Content(@thumbnail.ThumbnailPicturePath)" alt="" width="200" /> 
     @Html.RadioButtonFor(o=>o.SelectedImage, @thumbnail.ImageGUID) Primary Picture 
     <!-- Checkbox to mark for deletion --> 
     @Html.CheckBoxFor(o=>thumbnail.Delete) Delete ???????? <!---- Here is a problem - I don't understand how this should work --> 
    } 
    <input id="Submit1" name="button" type="submit" value="Complete Edit!" /> 
} 

- MyController.cs -

[HttpPost] 
public ActionResult Edit(String button, HttpPostedFileBase file, MyMainModel model) 
{ 
    // if button = submit picture, work with picture here and break(long story) 

    // save model data 
     // if valid, save and redirect 


    // not valid or error, load up view like normal but with error messages 
    model.LoadThumbnails(); 
    return View(model); 

} 

- MyMainModel.cs - -

public class MyMainModel 
{ 
    // some properties... 
    public Guid? SelectedImage { get; set; } 

    [Display(Name = "Detailed Description")] 
    public String LongDescription { get; set; } 

    // some more properties.... 


    // and finally my list of models 
    public IList<ThumbnailModel> ThumbnailImagePathAndNames { get; set; } 

    public void LoadThumbnails() 
    { 
     // load up initial thumbnail models 
     this.ThumbnailImagePathAndNames = new List<ThumbnailModel>(readDataService.GetThumbnailModels(this.SomeID)); 
    } 
} 

- Hình thu nhỏModel s.cs -

public class ThumbnailModel 
{ 
    public Guid ImageGUID { get; set; } 
    public String FullSizePicturePath { get; set; } 
    public String ThumbnailPicturePath { get; set; } 

    public bool Delete { get; set; } 
} 

Vậy vấn đề là gì? Vâng, khi "Hoàn thành chỉnh sửa!" nút được nhấn, chỉnh sửa MyController được gọi là, như mong đợi với tất cả các dữ liệu của MyMainModle trong tact .... ngoại trừ danh sách của ThumbnailModel's - những hóa ra là null.

Việc này phải được thực hiện như thế nào? Tôi đã thử nhiều cách tiếp cận khác nhau bao gồm tạo mẫu có thể chỉnh sửa và sử dụng EditFor (o => ... tất cả để không có kết quả (điều này trở nên khó hiểu vì tôi không biết liệu EditFor có được cho toàn bộ bộ sưu tập hay không tất cả được sử dụng để làm việc cho đến khi tôi thêm vào sự phức tạp của hộp kiểm để xóa, do đó cần phải truy xuất danh sách các ThumbnailModels để kiểm tra giá trị thuộc tính Delete bên trong đó để kiểm tra giá trị thuộc tính Delete nội bộ đó. tất cả để đọc và cố gắng hiểu điều này.

[Tuyên bố từ chối trách nhiệm - một số tên biến và phương pháp đã được thay đổi để bảo vệ chương trình vô tội. Nhiều mã đã bị tước đi và được thay thế bằng mã nhận xét.]

+0

Không nên là 'thumbnail => thumbnail.Delete'? – Buildstarted

Trả lời

45

Dưới đây là một ví dụ mà tôi đã đặt để minh họa một số khái niệm:

mẫu:

public class MyMainModel 
{ 
    public Guid? SelectedImage { get; set; } 
    public string LongDescription { get; set; } 

    public IEnumerable<ThumbnailModel> ThumbnailImagePathAndNames { get; set; } 

    public HttpPostedFileBase File { get; set; } 
} 

public class ThumbnailModel 
{ 
    public Guid ImageGUID { get; set; } 
    public bool Delete { get; set; } 
} 

Bộ điều khiển:

public class HomeController : Controller 
{ 
    public ActionResult Index() 
    { 
     var model = new MyMainModel 
     { 
      // TODO: fetch from the repository instead of hardcoding 
      ThumbnailImagePathAndNames = new[] 
      { 
       new ThumbnailModel { ImageGUID = Guid.NewGuid() }, 
       new ThumbnailModel { ImageGUID = Guid.NewGuid() }, 
       new ThumbnailModel { ImageGUID = Guid.NewGuid() }, 
      } 
     }; 
     return View(model); 
    } 

    [HttpPost] 
    public ActionResult Index(MyMainModel model) 
    { 
     ... the model will be properly bound here 
    } 
} 

Xem:

@model AppName.Models.MyMainModel 
@{ 
    ViewBag.Title = "Index"; 
    Layout = "~/Views/Shared/_Layout.cshtml"; 
} 
@using (Html.BeginForm("index", "home", FormMethod.Post, new { enctype = "multipart/form-data" })) 
{ 
    <div class="editor-field"> 
     @Html.TextAreaFor(m => m.LongDescription) 
     @Html.ValidationMessageFor(m => m.LongDescription) 
    </div> 
    <input type="file" name="file" /> 
    <!-- Use different names for the upload and complete submit 
     buttons so that you can distinguish which one was clicked 
     in the POST action 
    --> 
    <input name="upload" type="submit" value="Add Picture" /> 

    @Html.EditorFor(x => x.ThumbnailImagePathAndNames)  
    <input name="complete" type="submit" value="Complete Edit!" /> 
} 

biên tập mẫu: (~/Views/Home/EditorTemplates/ThumbnailModel.cshtml):

@model AppName.Models.ThumbnailModel 
<!-- Pass the image id as hidden field --> 
@Html.HiddenFor(x => x.ImageGUID) 
@Html.CheckBoxFor(x => x.Delete) 
+0

Tuyệt vời! Nó hoạt động !! Cảm ơn bạn rất nhiều - điều đó cực kỳ hữu ích. Điều cuối cùng tôi đang cố gắng để làm việc với ví dụ này là tôi có một bố cục phức tạp hơn cho mỗi ThumbnailModels. Trong mã ban đầu của tôi, tôi sử dụng một cái gì đó như @foreach (var hình thu nhỏ trong Model.ThumbnailImagePathAndNames) ... Có vẻ như tôi không thể đặt bố trí của tôi (cột giới hạn thứ) mã xung quanh chỉnh sửa cho và sau đó sử dụng @ Html.EditorFor (x => hình thu nhỏ). Bạn có biết làm thế nào tôi có thể có được kiểm soát hạt mịn này? Trong mẫu bằng cách nào đó đặt @model IEnumerable ở trên cùng? Cảm ơn! – Rob

+0

** Tôi có thể phải chuyển câu hỏi trên sang chủ đề mới. Để diễn giải, tôi muốn có nhiều quyền kiểm soát hơn đối với @ Html.EditorFor, sử dụng @Foreach của riêng tôi để xử lý từng mô hình riêng biệt, thay vì cho phép .NET lặp lại và tự động hiển thị.Thay đổi mẫu trình chỉnh sửa để thực hiện @model IEnumerable và triển khai @foreach bên trong đã chứng minh không thành công. – Rob

+0

@Rob, tôi không hiểu bạn cần loại kiểm soát nào. Trong ví dụ của bạn, bạn chỉ đang lặp qua bộ sưu tập và cho mỗi phần tử bạn xuất ra một vài trường nhập. EditorFor là đủ để tránh bạn viết tất cả điều này. –

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