Reputation: 3447
I have a PartialView that is an image upload, and basically I am displaying some images and then the normal Upload buttons :-
@model MvcCommons.ViewModels.ImageModel
<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", FormMethod.Post, new { enctype = "multipart/form-data" })) {
<input type="file" name="file" />
<input type="submit" value="submit" /> <br />
<input type="text" name="description" />
}
Now my idea is to have this in different pages. I tried it in 1 page already and is working fine, however when I Upload an image,
public ActionResult ImageUpload()
{
ImageModel model = new ImageModel();
model.Populate();
return View(model);
}
I want to go back to the "previous" View, ie the View that is hosting this partial view? When I do return View(model)
as above, I get into the ImageUpload
partial view which I do not want to.
Thanks for your help and time.
***UPDATE********* I went for the simple route for the time being, and hard coded the actual View name
public ActionResult ImageUpload()
{
ImageModel model = new ImageModel();
model.Populate();
return View("~/Views/Project/Create.cshtml", model);
}
however I got an error :-
The model item passed into the dictionary is of type MvcCommons.ViewModels.ImageModel
, but this dictionary requires a model item of type MvcCommons.Models.Project
.
Upvotes: 0
Views: 475
Reputation: 2456
Use the overload that takes a string of the name of the view you want.
http://msdn.microsoft.com/en-us/library/dd460310
protected internal ViewResult View(
string viewName,
Object model
)
i.e.
return View("ViewName", model);
if you have this in different pages then you can inject context via the action paramaters;
public ActionResult ImageUpload(string parentViewName)
{
ImageModel model = new ImageModel();
model.Populate();
return View(parentViewName, model);
}
NOTE: You should only need to pass the views name not the path:
return View("Create", model);
Upvotes: 2