共享部分视图 ActionResult 问题

发布于 2024-12-01 11:37:38 字数 598 浏览 1 评论 0原文

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

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

更新。这是部分视图:

@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>

}

I am trying to return the parent View when I call an ActionResult from within a Partial View. If I know the parent view I can just type in return View("Index");, but I can't because their could be multiple parent views as this partial view is shared... So how do I return the correct parent View in the ActionResult?

This seems so simple, but it has me stumped...

Update. Here is the Partial View:

@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>

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风柔一江水 2024-12-08 11:37:38

我假设您在视图中使用 @{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);
}

I assume you're using @{Html.RenderAction("Action");} in your view to call

[ChildActionOnly]
public ActionResult Action()
{
    var model = context.Data.Where(x => x);

    return PartialView("PartialView", model);
}

if so, then you could specify your routeValues as well.
In your view you would call

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

and in you controller:

[ChildActionOnly]
public ActionResult Action(string viewName)
{
    var model = context.Data.Where(x => x);

    if (model == null) return View(viewName);

    return PartialView("PartialView", model);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文