[英]ASP.NET MVC3 Upload a file from a Partial view (and fill the corresponding field in the Model)
I know the subject has already been discussed on SO and elsewhere, but I can't find any answer to my question. 我知道这个问题已经在SO和其他地方讨论过了 ,但我找不到任何问题的答案。
I'm working on an ASP.NET MVC3 project, and I'd like to create a Partial view containing a FileUpload
. 我正在开发一个ASP.NET MVC3项目,我想创建一个包含
FileUpload
的Partial视图 。 This partial view is called on a basic Create
page, and I'd like the file to upload belongs to the model to create. 在基本的
Create
页面上调用此局部视图,我希望要上载的文件属于要创建的模型。 It is only when the user will submit the form the selected file will be uploaded. 只有当用户提交表单时,才会上传所选文件。
Here is an explanation by the code : 以下是代码的解释:
Model ModelToCreate
Model
ModelToCreate
public class ModelToCreate
{
//Some properties
public FileUploadModel Files { get; set; }
}
Model FileUploadModel
模型
FileUploadModel
public class FileUploadModel
{
public IEnumerable<HttpPostedFileBase> Files { get; set; }
}
My PartialView (_UploadFiles.cshtml) 我的PartialView(_UploadFiles.cshtml)
@model Models.ModelToCreate
//I tried with Html.BeginForm(Create, MyController instead of null, null, but with no result.
@using (Html.BeginForm(null, null, FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.TextBoxFor(m => m.Files, new { type = "file", name = "Files" })
}
How the partial view is called (by Create.cshtml) 如何调用局部视图(通过Create.cshtml)
@Html.Partial("_UploadFiles")
I also tried with @Html.Partial("_UploadFiles", Model)
, but with no result... 我也试过
@Html.Partial("_UploadFiles", Model)
,但没有结果......
When I click the Submit
button in Create.cshtml
, the form is submitted to my controller BUT Files
field is always null
whereas the other datas are OK. 当我单击
Create.cshtml
的“ Submit
按钮时,表单将提交给我的控制器。“ Files
字段始终为null
而其他数据则为“正常”。
Am I missing something ? 我错过了什么吗? Could you indicate my where (and why ?)
你能指出我的位置(为什么?)
Thank you ! 谢谢 !
UPDATE (and Solution) 更新(和解决方案)
Here is some additionnal information I forgot about Create.cshtml
The form look like this : 这里有一些我忘记了
Create.cshtml
的附加信息表单如下所示:
@using (Html.BeginForm(null, null, FormMethod.Post, new { id = "target-form" }))
{
// Some fields, Textboxes and others CheckBoxes
//Call to partial View
}
When I take a look to generated source code, I see the partial view in a <form>
tag... So I have a <tag>
in a <tag>
, which is illegal and "ignored". 当我看一下生成的源代码时,我在
<form>
标签中看到了局部视图...所以我在<tag>
中有一个<tag>
,这是非法的并且“被忽略”。 This causes the problem 这导致了问题
SOLUTION Just add this tag to the BeginForm of Create.cshtml
: 解决方案只需将此标记添加到Create.cshtml的
Create.cshtml
:
@using (Html.BeginForm(null, null, FormMethod.Post, new { id = "target-form", enctype = "multipart/form-data" }))
and call my Partial View as 并将我的部分视图称为
@Html.Partial("_UploadFiles", Model)
Ok, this will get it working for you. 好的,这将使它适合你。
Create.cshtml
View (With the form & submit moved outside of the partial) Create.cshtml
视图(将表单和提交移动到部分之外)
@using(Html.BeginForm(null, null, FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.Partial("_UploadFiles", Model)
<input type="submit" value="submit" />
}
_UploadFiles.cshtml
view _UploadFiles.cshtml
视图
@model ModelToCreate
@Html.TextBoxFor(m => m.Files.Files, new { type = "file", name = "Files" })
Models (Changed to a List and also note the initializer in the FileUploadModel
constructor). 模型(更改为列表并注意
FileUploadModel
构造函数中的初始化程序)。
public class ModelToCreate
{
//Some properties
public FileUploadModel Files { get; set; }
}
public class FileUploadModel
{
public FileUploadModel()
{
Files = new List<HttpPostedFileBase>();
}
public List<HttpPostedFileBase> Files { get; set; }
}
Controller actions: 控制器动作:
public ActionResult Create()
{
var model = new ModelToCreate();
return View(model);
}
[HttpPost]
public ActionResult Create(ModelToCreate model)
{
var file = model.Files.Files[0];
return View(model);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.