基本上我有一个Image Upload控制器,我在页面中插入如下: -
<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?
Thansk的帮助和时间。
的 的 ** * ** * ** * 的更新 * ** * ** * ** * ** * ** * ** * ** * *** 上传后,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");
}
答案 0 :(得分:1)
您可以将projectId
作为RedirectToAction
的路线值传递。您应该更改ImageUpload
操作以接受projectId
。
public ActionResult Save(int projectId)
{
....
return RedirectToAction("ImageUpload", new { projectId = projectId });
}
public ActionResult ImageUpload(int projectId)
{
var model = .. get the model from db based on projectId
return View("view name", model);
}