刷新/重新加载当前视图中的部分视图

时间:2012-06-01 12:33:47

标签: asp.net-mvc asp.net-mvc-3 c#-4.0 razor partial-views

我有一个图像上传的PartialView,基本上我正在显示一些图像,然后是正常的上传按钮: -

@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" /> 
}

现在我的想法是将它放在不同的页面中。我已经在1页中尝试了它并且工作正常,但是当我上传图像时,

public ActionResult ImageUpload()
{
    ImageModel model = new ImageModel();
    model.Populate();
    return View(model);
}

我想回到“上一个”视图,即托管此局部视图的视图?当我按照上述return View(model)进行操作时,我会进入ImageUpload部分视图,我不想这样做。

感谢您的帮助和时间。

*** 更新 * ** * ** * ** 我暂时选择了简单的路线,并对实际的视图名称进行了硬编码

public ActionResult ImageUpload()
{
    ImageModel model = new ImageModel(); 
    model.Populate(); 
    return View("~/Views/Project/Create.cshtml", model); 
}

然而我收到了一个错误: -

传递到字典中的模型项的类型为MvcCommons.ViewModels.ImageModel,但此字典需要类型为MvcCommons.Models.Project的模型项。

1 个答案:

答案 0 :(得分:2)

使用带有所需视图名称字符串的重载。

http://msdn.microsoft.com/en-us/library/dd460310

protected internal ViewResult View(
        string viewName,
        Object model
)

return View("ViewName", model);

如果你在不同的页面中有这个,那么你可以通过动作参数注入上下文;

public ActionResult ImageUpload(string parentViewName)
{
    ImageModel model = new ImageModel();
    model.Populate();
    return View(parentViewName, model);
}

注意:您只需要传递视图名称而不是路径:

return View("Create", model);
相关问题