MVC3, multiple file upload, model binding

asp.net-mvc-3 entity-framework file-upload model-binding

Question

There is a view to update a complex model(Transaction). Complex model has properties which can have multiple attachments(files), so that user can upload multiple files simultaneously in this form, and I am trying to save these files to the database.

I have successfully posted multiple files to the server, following blog post http://haacked.com/archive/2010/07/16/uploading-files-with-aspnetmvc.aspx.

However in order to save these files, so that I can keep track of which files belongs to which object of the complex model(Transaction) and therefore show them later at appropriate places, I need some way to associate file uploaded to the object it belongs to, but since all files come under name 'files' I don't know how I can make this work.

Here is simplified complex model:

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

    public virtual PurchaseRequisition PurchaseRequisition { get; set; }

    public virtual Evaluation Evaluation { get; set; }
}

Properties of complex model:

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

Here is the controller:

[HttpPost]
public ActionResult Create(TransactionViewModel model, IEnumerable<HttpPostedFileBase> files)
{ //save to database }
1
6
3/9/2012 4:33:07 PM

Accepted Answer

Create separate sections in the view for the purchase requisitions and bid results. Something like this:

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

Then you would have an action signature like this:

[HttpPost]
public ActionResult Create(
  TransactionViewModel model, 
  IEnumerable<HttpPostedFileBase> purchasereqs,
  IEnumerable<HttpPostedFileBase> bidresults)
{ 
   //save to database 
}
8
2/21/2015 4:35:10 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow