Reputation: 3447
Basically I have an Image Upload controller, that I am inserting in pages as follows :-
<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>
So I am passing an ID to the ImageUpload, in this case the ProjectID, so that I can include it in my insert.
Now this is piece of code is populating an ImageModel(id), in my case its 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);
}
}
and this in turn leads to the 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" />
}
So far so good, however my problem is that the first time
new { ProjectID = Model.ProjectID }
is correctly populated with the ProjectID, however, when I upload an image, the ProjectID is lost, and becomes zero. Is there a way I can persist the ProjectID for the second time?
Thansk for your help and time.
********* UPDATE ************************* After the upload, the Action is as follows inside the FileController :-
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");
}
Upvotes: 0
Views: 365
Reputation: 32758
You can pass the projectId
as a route value from the RedirectToAction
. You should change the ImageUpload
action to accept the 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);
}
Upvotes: 1