MVC3, carga de varios archivos, enlace de modelos

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

Aquí está el controlador:

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

preguntado el 09 de marzo de 12 a las 16:03

1 Respuestas

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 
}

Respondido 21 Feb 15, 16:02

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.