基本上我有一个图像上传控制器,我在页面中插入如下: -
<div id='imageList'>
<h2>Upload Image(s)</h2>
@{
if (Model != null)
{
Html.RenderPartial("~/Views/File/ImageUpload.cshtml", new MvcCommons.ViewModels.ImageModel(Model.Project.ProjectID));
}
else
{
Html.RenderPartial("~/Views/File/ImageUpload.cshtml", new MvcCommons.ViewModels.ImageModel(0));
}
}
</div>
所以我将一个 ID 传递给 ImageUpload,在本例中为 ProjectID,以便我可以将它包含在我的插入中。
现在这是一段代码正在填充 ImageModel(id),在我的例子中是它的 ProjectID:-
public ImageModel(int projectId)
{
if (projectId > 0)
{
ProjectID = projectId;
var imageList = unitOfWork.ImageRepository.Get(d => d.ItemID == projectId && d.PageID == 2);
this.AddRange(imageList);
}
}
这反过来又导致 ImageUploadView.cshtml :-
<table>
@if (Model != null)
{
foreach (var item in Model)
{
<tr>
<td>
<img src= "@Url.Content("/Uploads/" + item.FileName)" />
</td>
<td>
@Html.DisplayFor(modelItem => item.Description)
</td>
</tr>
}
}
</table>
@using (Html.BeginForm("Save", "File", new { ProjectID = Model.ProjectID },
FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<input type="file" name="file" />
<input type="submit" value="submit" /> <br />
<input type="text" name="description" />
}
到目前为止一切顺利,但是我的问题是第一次
new { ProjectID = Model.ProjectID }
正确填充了 ProjectID,但是,当我上传图像时,ProjectID 丢失并变为零。有没有办法第二次保留 ProjectID?
感谢您的帮助和时间。
** * ** * ** * UPDATE * ** * ** * ** * ** * ** * ** * ** * *** 上传后,FileController 内部的 Action 如下:-
public ActionResult Save(int ProjectID)
{
foreach (string name in Request.Files)
{
var file = Request.Files[name];
string fileName = System.IO.Path.GetFileName(file.FileName);
Image image = new Image(fileName, Request["description"]);
ImageModel model = new ImageModel();
model.Populate();
model.Add(image, file);
}
return RedirectToAction("ImageUpload");
}