6

Có chế độ xem để cập nhật mô hình phức tạp (Giao dịch). Mô hình phức tạp có các thuộc tính có thể có nhiều tệp đính kèm (tệp), để người dùng có thể tải lên nhiều tệp cùng một lúc trong biểu mẫu này, và tôi đang cố gắng lưu các tệp này vào cơ sở dữ liệu.MVC3, nhiều tệp tải lên, mô hình ràng buộc

Tôi đã đăng thành công nhiều tệp lên máy chủ, theo bài đăng trên blog http://haacked.com/archive/2010/07/16/uploading-files-with-aspnetmvc.aspx.

Tuy nhiên để lưu các tệp này, để tôi có thể theo dõi tệp nào thuộc đối tượng của mô hình phức tạp (Giao dịch) và do đó hiển thị chúng sau này tại các địa điểm thích hợp, tôi cần một số cách để liên kết tệp được tải lên đối tượng nó thuộc về, nhưng vì tất cả các tệp đều nằm dưới tên 'tệp', tôi không biết làm thế nào tôi có thể thực hiện công việc này.

đây được đơn giản hóa mô hình phức tạp:

public class Transaction 
{ 
    [Key] 
    public int Id { get; set; } 

    public virtual PurchaseRequisition PurchaseRequisition { get; set; } 

    public virtual Evaluation Evaluation { get; set; } 
} 

Tính chất của mô hình phức tạp:

public class PurchaseRequisition 
{ 
    [Key, ForeignKey("Transaction")] 
    public int TransactionId { get; set; } 

    public virtual Transaction Transaction { get; set; } 

    [Display(Name = "Specifications/Requisitioner's Notes")] 
    public virtual ICollection<Attachment> SpecsRequisitionerNotesFiles { get; set; } 
} 

public class Evaluation 
{ 
    [Key, ForeignKey("Transaction")] 
    public int TransactionId { get; set; } 

    public virtual Transaction Transaction { get; set; } 

    public virtual ICollection<Attachment> BidResultsFiles { get; set; } 
} 

public abstract class Attachment 
{ 
    [Key] 
    public int Id { get; set; } 

    public string FileName { get; set; } 

    public string FileExtension { get; set; } 

    public byte[] Data { get; set; } 

    public Boolean Deleted { get; set; } 
} 

Dưới đây là bộ điều khiển:

[HttpPost] 
public ActionResult Create(TransactionViewModel model, IEnumerable<HttpPostedFileBase> files) 
{ //save to database } 

Trả lời

8

Tạo phần trong chế độ xem để mua các yêu cầu và kết quả đấu thầu. Một cái gì đó như thế này:

<form action="" method="post" enctype="multipart/form-data"> 

    <h3>Purchase Requistions</h3> 
    <label for="file1">Filename:</label> 
    <input type="file" name="purchasereqs" id="file1" /> 

    <label for="file2">Filename:</label> 
    <input type="file" name="purchasereqs" id="file2" /> 

    <h3>Bid Results</h3> 
    <label for="file3">Filename:</label> 
    <input type="file" name="bidresults" id="file3" /> 

    <label for="file4">Filename:</label> 
    <input type="file" name="bidresults" id="file4" /> 

    <input type="submit" /> 
</form> 

Sau đó, bạn sẽ có một chữ ký hành động như thế này:

[HttpPost] 
public ActionResult Create(
    TransactionViewModel model, 
    IEnumerable<HttpPostedFileBase> purchasereqs, 
    IEnumerable<HttpPostedFileBase> bidresults) 
{ 
    //save to database 
} 
+0

đó đã làm việc một cách hoàn hảo. Cảm ơn bạn!! – ljustin

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