C# Asp.net MVC 2 Request.files[“始终”返回空值

C# Asp.net MVC 2 Request.files[“始终”返回空值,c#,asp.net-mvc-2,image-upload,C#,Asp.net Mvc 2,Image Upload,这是控制器代码。我总是在那里得到request.files null。 要上传图像并将其存储到数据库中,我需要做哪些更改。修改razor代码,如下所示 [HttpPost] public ActionResult Create(StudentModel StudentEn,HttpPostedFileBase File1) { Student st = new Student(); HttpPostedFileBase file;

这是控制器代码。我总是在那里得到request.files null。
要上传图像并将其存储到数据库中,我需要做哪些更改。

修改razor代码,如下所示

[HttpPost]
public ActionResult Create(StudentModel StudentEn,HttpPostedFileBase File1) 
        {
            Student st = new Student();
            HttpPostedFileBase file;

           file = Request.Files["File1"];///it only access the name of the object of html."file1" is the name of the object

            if (file != null && file.ContentLength > 0)
            {
                byte[] Image=null;
                Image = new byte[file.ContentLength];
                file.InputStream.Read(Image, 0, file.ContentLength);
               StudentEn.Image=Image;
            }
            if (ModelState.IsValid)
            {
                st.Insert(StudentEn);
                return RedirectToAction("Index");
            }
            else
            {
                StudentEn.STDs = getSelectedSTD(GetSTDList());
                return View(StudentEn);
            }
        }       
请尝试下面的razor代码

[HttpPost]
public ActionResult Create(StudentModel StudentEn,HttpPostedFileBase File1) 
        {
            Student st = new Student();
            HttpPostedFileBase file;

           file = Request.Files["File1"];///it only access the name of the object of html."file1" is the name of the object

            if (file != null && file.ContentLength > 0)
            {
                byte[] Image=null;
                Image = new byte[file.ContentLength];
                file.InputStream.Read(Image, 0, file.ContentLength);
               StudentEn.Image=Image;
            }
            if (ModelState.IsValid)
            {
                st.Insert(StudentEn);
                return RedirectToAction("Index");
            }
            else
            {
                StudentEn.STDs = getSelectedSTD(GetSTDList());
                return View(StudentEn);
            }
        }       
试试这个:

[HttpPost]
    public ActionResult Create(StudentModel StudentEn,HttpPostedFileBase File1) 
            {
                Student st = new Student();
                //HttpPostedFileBase file;

              // file = Request.Files["File1"];///it only access the name of the
                                //object of html."file1" is the name of the object

                if (File1 != null && File1.ContentLength > 0)
                {
                    byte[] Image=null;
                    Image = new byte[File1.ContentLength];
                    File1.InputStream.Read(Image, 0, File1.ContentLength);
                   StudentEn.Image=Image;
                }
                if (ModelState.IsValid)
                {
                    st.Insert(StudentEn);
                    return RedirectToAction("Index");
                }
                else
                {
                    StudentEn.STDs = getSelectedSTD(GetSTDList());
                    return View(StudentEn);
                }
            } 

问题是,您的页面中有两个表单,这让人困惑

尝试删除/注释第一个表单


i、 e.

在方法
Create()
中,第二个参数
HttpPostedFileBase File1
用于什么?我根本看不到您的代码引用它…为什么要将文件1分配给文件..只需使用文件1而不是文件并检查结果..尝试
Request.Files[0]
我建议您不要将Webforms与MVC混合使用。(即:
它不起作用我根据@Sachu的建议尝试了,但它给出了File1 null。然后根据@erikphilip尝试避免将webforms与MVC一起使用..使用MVC语法..因为相同的代码对我来说与MVC razor格式相同,所以我在这个项目中使用了MVC 2,所以使用了webforms语法。我已经根据你的建议更改了aspx文件,但仍然是结果是一样的。
[HttpPost]
    public ActionResult Create(StudentModel StudentEn,HttpPostedFileBase File1) 
            {
                Student st = new Student();
                //HttpPostedFileBase file;

              // file = Request.Files["File1"];///it only access the name of the
                                //object of html."file1" is the name of the object

                if (File1 != null && File1.ContentLength > 0)
                {
                    byte[] Image=null;
                    Image = new byte[File1.ContentLength];
                    File1.InputStream.Read(Image, 0, File1.ContentLength);
                   StudentEn.Image=Image;
                }
                if (ModelState.IsValid)
                {
                    st.Insert(StudentEn);
                    return RedirectToAction("Index");
                }
                else
                {
                    StudentEn.STDs = getSelectedSTD(GetSTDList());
                    return View(StudentEn);
                }
            } 
public ActionResult Create(StudentModel StudentEn,FormCollection formCollection) 
{
      HttpPostedFileBase file= formCollection.Get("File1");
}