【问题标题】:refreshing / reloading the PartialView inside the current view在当前视图中刷新/重新加载 PartialView
【发布时间】:2012-06-01 12:33:47
【问题描述】:

我有一个 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 类型的模型项。

【问题讨论】:

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


    【解决方案1】:

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

    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);
    

    【讨论】:

    • 嗨 Jflood,我应该在哪个控制器中执行此操作? ImageUpload 不能,所以我怎样才能获得以前的视图名称?这个局部视图将在不同的视图中。
    • 添加了更多信息:试试最后一个,让我知道它是否有效?
    • 嗨,Jflood,我尝试了最后一个,但 ParentActionViewContext 为空。我还使用您上面建议的示例更新了我的问题,但是我暂时对其进行了硬编码以使其正常工作。虽然仍然是一个错误
    • 我想通过 2 个模型,但我认为这是不可能的。一个正在检索项目的模型,以及一个正在检索图像的另一个模型。否则我必须做一个组合,但是对于我拥有的每个实体,因为它们中的大多数都会有图像。
    • 是的,不确定你提到的“字典”在代码中的位置。如果你需要的话,在动作中组合模型。
    猜你喜欢
    • 2012-06-20
    • 1970-01-01
    • 2019-04-12
    • 1970-01-01
    • 2019-09-24
    • 1970-01-01
    • 2018-09-05
    • 2011-04-26
    • 2018-11-05
    相关资源
    最近更新 更多