6

복잡한 모델 (트랜잭션)을 업데이트하는보기가 있습니다. 복잡한 모델에는 이라는 여러 첨부 파일 (파일)을 가질 수있는 속성이있어서 사용자가 이 양식으로 동시에 여러 파일을 업로드 할 수 있으며 이러한 파일을 데이터베이스에 저장하려고합니다.MVC3, 여러 파일 업로드, 모델 바인딩

http://haacked.com/archive/2010/07/16/uploading-files-with-aspnetmvc.aspx 블로그 게시물 에 여러 파일을 게시했습니다.

그러나 이러한 파일을 저장하기 위해 어느 파일이 복잡한 모델 (트랜잭션)의 어떤 개체에 속해 있는지 추적하여 나중에 적절한 위치에 표시 할 수 있도록 업로드 된 파일을 연결하는 방법이 필요합니다. 그것이 속한 객체이지만 모든 파일이 '파일'이라는 이름 아래에 있기 때문에이 작업을 어떻게 수행 할 수 있는지 알지 못합니다.

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

답변

8

가 구매의도에 별도의 섹션을 만듭니다 복잡한 모델의

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

    public virtual PurchaseRequisition PurchaseRequisition { get; set; } 

    public virtual Evaluation Evaluation { get; set; } 
} 

등록 : 여기

복잡한 모델을 단순화 컨트롤러 여기

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; } 
} 

입니다 요청 및 입찰 결과. 이런 식으로 뭔가 :

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

는 그런 다음 작업 서명과 같은 것이다 : 완벽하게 작동

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

. 고맙습니다!! – ljustin