在MVC 5中上传图像时,asp.net-mvc – Request.Files.Count总是为0
发布时间:2020-05-30 16:41:18 所属栏目:asp.Net 来源:互联网
导读:我有一个Post控件在一个模型与一些字符串字段和一个图像.完全相同的代码适用于MVC4,但在MVC 5中,Request.Files.Count始终为0 我的模型有byte [],而不是HttpPostedFileBase 我的看法: @using (Html.BeginForm(Create, HotelManager, FormMethod.Post, new { e
|
我有一个Post控件在一个模型与一些字符串字段和一个图像.完全相同的代码适用于MVC4,但在MVC 5中,Request.Files.Count始终为0 我的模型有byte [],而不是HttpPostedFileBase 我的看法: @using (Html.BeginForm("Create","HotelManager",FormMethod.Post,new { enctype = "multipart/form-data",data_ajax = "false" }))
{
@Html.AntiForgeryToken()
@Html.TextBoxFor(model => model.Title,new { @class = "form-control" })
@Html.TextAreaFor(model => model.Description,new { @class = "form-control",@rows = "7" })
<input type="file" class="form-control filestyle">
<input type="submit" value="Submit" class="btn btn-block" />
}
我已经尝试省略了data_ajax =“false”,但没有用. 我的邮政主管如下: [HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "Title,Description,Image")] Hotel hotel)
{
if (ModelState.IsValid)
{
// deal with the uploaded file
if (Request.Files.Count > 0)
{
HttpPostedFileBase file = Request.Files[0];
if (file != null && file.ContentLength > 0)
{
// Code to process image,resize,etc goes here
}
}
// Other code to add the hotel to db goes here
}
在调试器中,我看到Request.Files.Count总是为零,我不知道为什么? Key Value Request POST /HotelManager/Create HTTP/1.1 Accept text/html,application/xhtml+xml,*/* Referer http://localhost:4976/HotelManager/Create Accept-Language en-US,en;q=0.5 User-Agent Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; Touch; ASU2JS; rv:11.0) like Gecko Content-Type multipart/form-data; boundary=---------------------------7de207070a24 Accept-Encoding gzip,deflate Host localhost:4976 Content-Length 946 DNT 1 在IE Developer窗口中,我看到窗体正文是空的. 解决方法我浪费了很多时间,事实证明,我需要做的只是使用name属性<input type="file" name="somename"> 我的新项目中缺少name属性. (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-mvc – 我何时以及为什么要考虑asp.net MVC?
- asp.net-mvc – 编译时mvc视图检查与msbuild
- asp.net-mvc – 了解nopCommerce的MVC标签
- asp-classic – 使用SMTP身份验证的经典ASP发送电子邮件
- ASP.NET MVC下Ajax.BeginForm方式无刷新提交表单实例
- Asp.net形成认证cookie,不符合IIS7超时
- asp.net-mvc – 使用NHibernate.AspNet.Identity
- asp.net – C#Web API模型绑定提供程序应如何工作?
- asp.net-mvc-3 – 使用redirectAction和prg模式在操作之间发
- asp.net – 在内容页面的主页面上设置属性值
推荐文章
站长推荐
- asp.net – 如何防止未经授权的蜘蛛侠
- asp.net-mvc-5 – MVC5中的域路由
- asp.net – 为什么IIS7需要很长时间
- 使用ASP.NET MVC捆绑和缩小模块化JavaScript(Req
- asp.net-mvc – ASP.NET MVC 5身份应用程序用户作
- asp.net – 如何获取SignalR用户连接ID在外线中心
- asp.net-mvc-3 – 剃刀不能轻松地使用强类型的Ht
- asp.net-mvc – 无法在Web服务器上启动调试. Web
- asp.net-mvc – 如何在ASP.net MVC 4 RouteConfi
- asp.net-mvc-3 – 使用jQuery验证货币字段的客户
热点阅读
