Fileupload работает только в действии Index (ASP.Net MVC) - PullRequest
0 голосов
/ 12 мая 2011

У меня есть простой контроллер с двумя действиями:

public class TestController : Controller
{
      // /Test
      public ActionResult Index()
      {
          return View();
      }
      [HttpPost]
      public ActionResult Index(HttpPostedFileBase file)
      {    
          bool b = file == null; //there will be false
          return RedirectToAction("Index");
      }

      // /Test/Wonder
      [HttpGet]
      public ActionResult Wonder()
      {
          return View();
      }        
      [HttpPost]
      public ActionResult Wonder(HttpPostedFile file)
      {
          bool b = file == null; //there will be TRUE!
          return RedirectToAction("Wonder");
      }
}

У меня похожие взгляды на мои действия. Индекс действия:

<h2>Index</h2>
<form action="" method="post" enctype="multipart/form-data">  
  <input type="file" name="file" id="file" />
  <input type="submit" />
</form>

Чудо-действие:

<h2>It's wonder!</h2>
<form action="" method="post" enctype="multipart/form-data">  
  <input type="file" name="file" id="file" />
  <input type="submit" />
</form>

Почему первая форма (индекс) передает правильный файл контроллеру, а вторая форма (Wonder) передает ноль контроллеру?

1 Ответ

1 голос
/ 12 мая 2011

Ваш индекс ActionResult получает в качестве параметра объект HttpPostedFileBase, тогда как Wonder ActionResult получает в качестве параметра объект HttpPostedFile.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...