【问题标题】:Shared Partial View ActionResult Issue共享部分视图 ActionResult 问题
【发布时间】:2011-08-25 21:20:07
【问题描述】:

当我从部分视图中调用 ActionResult 时,我试图返回父视图。如果我知道父视图,我可以输入 return View("Index");,但我不能,因为它们可能是多个父视图,因为这个部分视图是共享的......那么我如何返回正确的父视图在 ActionResult 中?

这看起来很简单,但它让我难住了......

更新。这是部分视图:

@model Website.Models.PostModel

@using (Html.BeginForm("SubmitPost", "Home", FormMethod.Post))
{
@Html.TextAreaFor(m => m.Message, 2, 50, null)
<br /><br />
<div class="ui-widget">
    Notes: @Html.TextBox("Notes", "", new { @class = "ui-autocomplete-input" })
</div>
<br />
<p>
    <input type="submit" value="Post" />
</p>

}

【问题讨论】:

    标签: asp.net-mvc partial-views


    【解决方案1】:

    我假设您在视图中使用@{Html.RenderAction("Action");} 来调用

    [ChildActionOnly]
    public ActionResult Action()
    {
        var model = context.Data.Where(x => x);
    
        return PartialView("PartialView", model);
    }
    

    如果是这样,那么您也可以指定您的 routeValues。 在您看来,您会调用

    @{Html.RenderAction("Action", new {viewName = "parentView"});}
    

    在你的控制器中:

    [ChildActionOnly]
    public ActionResult Action(string viewName)
    {
        var model = context.Data.Where(x => x);
    
        if (model == null) return View(viewName);
    
        return PartialView("PartialView", model);
    }
    

    【讨论】:

    • 我使用 HTML.BeginForm 作为我的提交按钮。我需要将父模型传递给局部视图吗?
    • 终于想通了。必须像您指出的那样将视图名称传递给部分渲染。感谢您的帮助!
    猜你喜欢
    • 2014-05-29
    • 2018-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多