Fileupload仅适用于索引操作(ASP.Net MVC) [英] Fileupload works in Index action only (ASP.Net MVC)
问题描述
我有两个简单的控制器动作:
$ public $ TestController $ Controller
/ //测试
public ActionResult Index()
{
return View();
[HttpPost]
public ActionResult Index(HttpPostedFileBase file)
{
bool b = file == null; //会出现false
return RedirectToAction(Index);
$ b // //测试/奇迹
[HttpGet]
public ActionResult Wonder()
{
return View();
[HttpPost]
public ActionResult Wonder(HttpPostedFile文件)
{
bool b = file == null; //会有真的!
返回RedirectToAction(Wonder);
$ b我有类似的看法,
索引操作:
< h2>索引< / h2>
< form action =method =postenctype =multipart / form-data>
< input type =filename =fileid =file/>
< input type =submit/>
< / form>
奇迹行动:
< h2>奇妙!< / h2>
< form action =method =postenctype =multipart / form-data>
< input type =filename =fileid =file/>
< input type =submit/>
< / form>为什么第一个窗体(Index)将正确的文件提交给控制器,而第二个窗体(Wonder)则提交了空的窗体($)?你的索引ActionResult收到一个HttpPostedFileBase对象的参数,而Wonder ActionResult收到一个HttpPostedFile对象作为参数。
I have simple controller with two actions:
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");
}
}
I have similar views for my actions.
Index action:
<h2>Index</h2>
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="file" id="file" />
<input type="submit" />
</form>
Wonder action:
<h2>It's wonder!</h2>
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="file" id="file" />
<input type="submit" />
</form>
Why does first form (Index) submit correct file to controller, but second form (Wonder) submits null to controller?
解决方案 Your Index ActionResult receives as a parameter a HttpPostedFileBase object whereas the Wonder ActionResult receives as a parameter a HttpPostedFile object.
这篇关于Fileupload仅适用于索引操作(ASP.Net MVC)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!