C# ASP.NET mvc3网页不会将文件上载到服务器

C# ASP.NET mvc3网页不会将文件上载到服务器,c#,asp.net-mvc-3,file-upload,C#,Asp.net Mvc 3,File Upload,我试图在我的数据库中存储一个类别对象,并上传一个我可以引用该类别的图像 类别存储完好,但由于某种原因,图像未被上传。当我调试时,我可以看到我的应用程序从未进入将文件存储在服务器上的方法,因为我的文件为“Null” 型号: using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Data.Entity; namespace SkyLearn.Areas.Ca

我试图在我的数据库中存储一个类别对象,并上传一个我可以引用该类别的图像

类别存储完好,但由于某种原因,图像未被上传。当我调试时,我可以看到我的应用程序从未进入将文件存储在服务器上的方法,因为我的文件为“Null”

型号:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;

namespace SkyLearn.Areas.Categories.Models
{
    public class Category
    {
        public int ID { get; set; }
        public string Title { get; set; }
        public string Icon { get; set; }
        public string Description { get; set; }
    }

    public class CategoryDBContext : DbContext
    {
        public DbSet<Category> categories { get; set; }
    }
}
视图:


创造
创造
类别
型号(名称)%>
型号(名称)%>
型号(名称)%>
型号(图标)%>
型号(说明)%>
型号(说明)%>
型号(说明)%>

如果有人能告诉我为什么不输入保存文件的方法,我将不胜感激

我试着在stackoverflow上查看其他答案,尽管其中一些答案与我的问题相同,但它们的解决方案并不能解决我的问题

我还尝试在我的config.web中更改上载大小等


请帮帮我:)

ModelBinder可能要爆炸了。当表单按原样设置时,您不能为名为
Icon
的操作方法输入内容,也不能在名为
Icon
类别上拥有一个名为
Icon
的属性(所有表单值都将与
类别
属性匹配)

此外,表单字段名是小写的“icon”,而不是C中匹配的大写“icon”#

您可能会创建一个自定义类,action方法将从表单post接收该类:

public class CategoryWithFile : Category
{
    public HttpPostedFileBase IconFile { get; set; }
}
然后将其作为动作方法上的参数

public ActionResult Create(CategoryWithFile category)
并将您的表格更改为:

 <div class="editor-label">
     <label for="IconFile">Icon: </label>
 </div>
 <div class="editor-field">
     <input type="file" name="IconFile" id="IconFile"/>
 </div>

偶像:

我不知道浏览器会如何处理嵌套表单,但使用
Html.BeginForm()
然后手动写出表单标记是没有意义的
Html.BeginForm()
具有用于输出Html属性的重载:

Html.BeginForm(action, controller, 
    FormMethod.Post, new { enctype="multipart/form-data" })

我不知道浏览器会如何处理嵌套表单,但使用
Html.BeginForm()
然后手动写出表单标记是没有意义的
Html.BeginForm()
有一个重载来输出Html属性:
Html.BeginForm(操作、控制器、FormMethod.Post、新的{enctype=“multipart/form data”})
。如果您愿意添加它作为答案,我将立即接受它。这就解决了我所有的问题:)
 <div class="editor-label">
     <label for="IconFile">Icon: </label>
 </div>
 <div class="editor-field">
     <input type="file" name="IconFile" id="IconFile"/>
 </div>
Html.BeginForm(action, controller, 
    FormMethod.Post, new { enctype="multipart/form-data" })