ind*_*imp 24 asp.net-mvc enctype http-headers asp.net-mvc-5 asp.net-mvc-5.1
我在模型上有一个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" />
}
Run Code Online (Sandbox Code Playgroud)
我试过省略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
}
Run Code Online (Sandbox Code Playgroud)
在调试器中,我看到Request.Files.Count始终为零,我不知道为什么?我已经复制了另一个MVC4项目的视图和控制器代码,它可以正常工作.
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
Run Code Online (Sandbox Code Playgroud)
在IE Developer窗口中,我看到表单体是空的.
ind*_*imp 45
我浪费了太多时间,事实证明我所需要做的就是使用name属性
<input type="file" name="somename">
Run Code Online (Sandbox Code Playgroud)
我的新项目中缺少name属性.
Ali*_*avi 40
您需要更改post方法
需要添加:
new { enctype = "multipart/form-data" }
Run Code Online (Sandbox Code Playgroud)
这是完整的表格标签
@using (Html.BeginForm("Cars", "Expense", FormMethod.Post,
new { enctype = "multipart/form-data" }))
Run Code Online (Sandbox Code Playgroud)
小智 12
要添加您需要更改post方法.
需要添加:
new { enctype = "multipart/form-data" }
Run Code Online (Sandbox Code Playgroud)
这是完整的表格标签
@using (Html.BeginForm("Cars", "Expense", FormMethod.Post,
new { enctype = "multipart/form-data" }))
Run Code Online (Sandbox Code Playgroud)
这些线条至关重要; 没有它们,您将无法保存类型文件的输入.我坐了6个小时试图解决这个问题.
| 归档时间: |
|
| 查看次数: |
32427 次 |
| 最近记录: |