asp.net-mvc-3 – MVC3,多文件上传,模型绑定
发布时间:2020-05-22 12:07:28 所属栏目:asp.Net 来源:互联网
导读:可以更新复杂模型(Transaction). 复杂模型具有可以具有多个附件(文件)的属性, 这样用户可以在这种形式下同时上传多个文件, 我试图将这些文件保存到数据库中. 我已成功将多个文件发布到服务器, 关注博文 http://haacked.com/archive/2010/07/16/uploading-file
|
可以更新复杂模型(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 – Web Forms MVP项目有哪些好的资源?
- asp.net – IIS在编译的.net站点中查找.cs文件
- asp-classic – 在VBScript中检查NULL的错误
- asp.net – 表单身份验证web.config设置
- asp.net – IE 11中的报表查看器打印按钮
- 在ASP.NET 4.5 WebForms中通过bundle.config和BundleConfig
- asp.net – 如果Ninject尚未绑定,如何在Ninject中绑定?
- asp.net – 如何获取当前登录用户的角色列表
- asp.net-mvc – HTML.Encode但保留换行符
- asp.net-mvc-4 – 提交相同的部分视图多次调用数据到控制器
推荐文章
站长推荐
- asp.net-mvc – Context.User.Identity.Name为nu
- ASP.NET JSON字符串与实体类的互转换示例代码
- 在ASP.Net中使用Page_Load和Page_PreRender
- 在ASP.net中组合和高速缓存多个JavaScript文件
- asp.net-mvc – 条件ASP.NET MVC剃刀部分
- asp.net-mvc-3 – MVC3不显眼的验证在IE中不起作
- asp.net-mvc – ASP.NET MVC – 值类型的自定义验
- 如何从ASP.NET Web服务生成JSONP以进行跨域调用?
- ASP.NET Web Forms 4.5模型绑定,其中模型包含一个
- asp.net-mvc – Visual Studio提示使用Razor语法
热点阅读
