asp.net-mvc-3 – MVC3,多文件上传,模型绑定
发布时间:2020-12-15 21:04:49 所属栏目:asp.Net 来源:网络整理
导读:可以更新复杂模型(Transaction). 复杂模型具有可以具有多个附件(文件)的属性, 这样用户可以在这种形式下同时上传多个文件, 我试图将这些文件保存到数据库中. 我已成功将多个文件发布到服务器, 关注博文 http://haacked.com/archive/2010/07/16/uploading-fil
可以更新复杂模型(Transaction).
复杂模型具有可以具有多个附件(文件)的属性, 这样用户可以在这种形式下同时上传多个文件, 我试图将这些文件保存到数据库中. 我已成功将多个文件发布到服务器, 但是为了保存这些文件,以便我可以跟踪哪些文件属于复杂模型(Transaction)的哪个对象,因此稍后在适当的位置显示它们,我需要一些方法将上传的文件关联到它所属的对象到,但由于所有文件都在名称’文件’下,我不知道如何使这项工作. 这是简化的复杂模型: 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; } } 这是控制器: [HttpPost] public ActionResult Create(TransactionViewModel model,IEnumerable<HttpPostedFileBase> files) { //save to database } 解决方法
在视图中为采购申请和投标结果创建单独的部分.像这样的东西:
<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 } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net – 使用httpModule手动启用压缩
- asp.net-mvc – MVC4 Web API或MVC3 JsonResult
- asp.net-mvc – 使用像Stackoverflow这样的查询字符串版本化
- asp.net – 如何使用&在web.config文件
- asp.net – 环境标记助手
- asp.net-mvc-3 – 在DEBUG或Debugger中运行时禁用MVC3中的O
- Asp.net core利用IIS在windows上进行托管步骤详解
- asp.net-mvc – Ajax.ActionLink使用背景不透明度加载图像定
- asp.net-core – 使用.net核心进行Hangfire依赖注入
- .NET Core多平台项目模板eShopOnContainers编译手记
推荐文章
站长推荐
热点阅读