C# 在MVC.NET中尝试使用表单发送文件时未设置对象引用

C# 在MVC.NET中尝试使用表单发送文件时未设置对象引用,c#,html,asp.net-mvc,file-upload,reference,C#,Html,Asp.net Mvc,File Upload,Reference,我有一个@Html.BeginForm(),它绑定到名为CreateQuestionViewModel的ViewModel。此ViewModel如下所示: public class CreateQuestionModel { public Question Question { get; set; } public List<int> PoliticianIds { get; set; } public List<int> TopicIds { get; se

我有一个
@Html.BeginForm()
,它绑定到名为CreateQuestionViewModel的ViewModel。此ViewModel如下所示:

public class CreateQuestionModel
{
  public Question Question { get; set; }
  public List<int> PoliticianIds { get; set; }
  public List<int> TopicIds { get; set; }
  public HttpPostedFile File { get; set; }
}
我希望有人能帮助我,因为我非常困惑

编辑

我的“创建获取”操作按请求执行:

public ActionResult Create(int userId = -1, int topicId = -1)
{
  ViewBag.polIds = manager.GetAllPoliticians();
  //ViewBag.Politicians = new MultiSelectList(manager.GetAllPoliticians(), "UserId", "FirstName");
  if (userId > -1)
  {
    ViewBag.AddressedPolitcian = manager.GetPolitician(userId);
  }
  ViewBag.AddressedId = userId;

  ViewBag.TopIds = manager.GetAllTopics();
  //ViewBag.Topics = new MultiSelectList(manager.GetAllTopics(), "TopicId", "Name");
  if (topicId > -1)
  {
    ViewBag.AddressedTopic = manager.GetTopic(topicId);
  }
  ViewBag.AddressedTopicId = topicId;

  ViewBag.Parties = manager.GetAllParties();

  return View();
}

在这个stackoverflow线程中找到了我以前没有找到的答案:。基本上,我在web.config中输入了max size,并使用HttpPostedFileBase而不是HttpPostedFile,显然您不能在ViewModel中使用file,所以您需要在Create POST方法中将其作为单独的参数输入

请发布您的get action以及您的解决方案代码,以及其他人的帮助。该错误刚刚消失,因此它是某种虚假消息。我会在有时间的时候发布我的解决方案;)谢谢你的帮助!一件事是在表单中设置enctype=“multipart/form data”,另一件事是设置HttpPostedFileBase
public ActionResult Create(CreateQuestionModel createQuestionModel)
{
  if (ModelState.IsValid)
  {
    if (createQuestionModel.File != null && createQuestionModel.File.ContentLength > 0)
    {
      String fileName = Path.GetFileName(createQuestionModel.File.FileName);

      String path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);

      createQuestionModel.File.SaveAs(path);
    }

    int id = WebSecurity.CurrentUserId;
    manager.CreateQuestion(createQuestionModel.Question, id, createQuestionModel.PoliticianIds, createQuestionModel.TopicIds);
    return RedirectToAction("Index", "Question", new { page = 1});
  }

  return View(createQuestionModel);
}
public ActionResult Create(int userId = -1, int topicId = -1)
{
  ViewBag.polIds = manager.GetAllPoliticians();
  //ViewBag.Politicians = new MultiSelectList(manager.GetAllPoliticians(), "UserId", "FirstName");
  if (userId > -1)
  {
    ViewBag.AddressedPolitcian = manager.GetPolitician(userId);
  }
  ViewBag.AddressedId = userId;

  ViewBag.TopIds = manager.GetAllTopics();
  //ViewBag.Topics = new MultiSelectList(manager.GetAllTopics(), "TopicId", "Name");
  if (topicId > -1)
  {
    ViewBag.AddressedTopic = manager.GetTopic(topicId);
  }
  ViewBag.AddressedTopicId = topicId;

  ViewBag.Parties = manager.GetAllParties();

  return View();
}