6

Karmaşık bir modeli (Transaction) güncellemek için görünüm vardır. Karmaşık model, birden çok eki (dosya), sahip olabilen özelliklere sahiptir, böylece kullanıcı bu formda aynı anda birden fazla dosya yükleyebilir, ve bu dosyaları veritabanına kaydetmeye çalışıyorum.MVC3, birden çok dosya yükleme, model bağlama

başarıyla blog post http://haacked.com/archive/2010/07/16/uploading-files-with-aspnetmvc.aspx ardından sunucuya birden fazla dosya gönderdiniz.

Ancak, bu dosyaları kaydetmek için hangi dosyaların karmaşık modelin (Transaction) hangi nesnesine ait olduğunu izleyebilmem ve bu nedenle bunları daha sonra uygun yerlerde gösterebilmem için karşıya yüklenen dosyayı ilişkilendirmenin bir yoluna ihtiyacım var ait olduğu nesne, ancak tüm dosyalar 'dosyalar' adı altında geldiğinden, bu işi nasıl yapabileceğimi bilmiyorum.

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

cevap

8

satın görünümünde ayrı bölümler oluşturun: Karmaşık modelin

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

    public virtual PurchaseRequisition PurchaseRequisition { get; set; } 

    public virtual Evaluation Evaluation { get; set; } 
} 

Özellikler ': İşte

kompleks modelini basitleştirilmiş kontrolör İşte

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

olduğunu talepler ve teklif sonuçları. Böyle bir şey:

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

Sonra bir eylem imzası böyle olurdu: mükemmel çalıştı

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

. Teşekkür ederim!! – ljustin

İlgili konular