【问题标题】:ASP.NET MVC3 RAZOR: Redirecting from Partial ViewsASP.NET MVC3 RAZOR:从部分视图重定向
【发布时间】:2012-02-23 12:53:05
【问题描述】:

我有两个部分视图“MyPopular”和“MyBlogs”。还有两个控制器——“ArticleController.cs”和“ThePopularController.cs”。这两个部分视图都包含按钮。

最初它在索引视图中呈现两个部分视图。

在博客点击的帖子操作处理程序中,它被要求重定向到“BlogHome”操作,它将返回一个简单的字符串“Blog Home”(而不是视图)。在流行点击的后操作处理程序上,它被要求重定向到“PopularHome”操作,它将返回一个简单的字符串“Popular Home”。但是目前,当我单击任何按钮时,它会呈现 localhost:1988/Article index;没有部分内容。

注意:即使我使用了 ContentResult 和 ActionResult,结果也是一样的。 注意:如果我通过错误的方式来完成如此简单的任务,请强调。

我们如何纠正它以进行正确的重定向?

//文章控制器

public class ArticleController : Controller
{

    public ActionResult Index()
    {
        //Index returns no model
        return View();
    }

    public string BlogHome()
    {
        return "Blog Home";
    }


    //ChildActionOnly attribute indicates that this action should not be callable directly via the URL. 
    [ChildActionOnly]
    public ActionResult MyBlogs()
    {
        Thread.Sleep(500);
        return PartialView(GetAllBlogEntries());
    }

    [ChildActionOnly]
    [HttpPost]
    public void MyBlogs(string blogclick)
    {
        RedirectToAction("BlogHome");
    }


    private IEnumerable<Blog> GetAllBlogEntries()
    {
        return new[]
                    {
                        new Blog { ID = 1, Head = "Introduction to MVC", PostBy = "Lijo", Content = "This is a ..." },
                        new Blog { ID = 2, Head = "jQuery Hidden Gems", PostBy = "Lijo", Content = "This is a ..." },
                        new Blog { ID = 3, Head = "Webforms Intenals", PostBy = "Lijo", Content = "This is a ..." }
                    };
    }



}

// ThePopularController

public class ThePopularController : Controller
{

    public string PoularHome()
    {
        return "Poular Home";
    }


    //ChildActionOnly attribute indicates that this action should not be callable directly via the URL. 
    [ChildActionOnly]
    public ActionResult MyPopular()
    {
        Thread.Sleep(500);
        return PartialView(GetPopularBlogs());
    }


    [ChildActionOnly]
    [HttpPost]
    public void MyPopular(string popularpress)
    {
        RedirectToAction("PoularHome");
    }


    private IEnumerable<PopularTutorial> GetPopularBlogs()
    {
        return new[]
                    {
                        new PopularTutorial { ID = 17, Title = "Test1", NumberOfReads = 1050 },
                        new PopularTutorial { ID = 18, Title = "Test2", NumberOfReads = 5550 },
                        new PopularTutorial { ID = 19, Title = "Test3", NumberOfReads = 3338 },
                        new PopularTutorial { ID = 20, Title = "Test4", NumberOfReads = 3338 },
                        new PopularTutorial { ID = 21, Title = "Test5", NumberOfReads = 3338 },
                        new PopularTutorial { ID = 22, Title = "Test6", NumberOfReads = 3338 },
                        new PopularTutorial { ID = 23, Title = "Test7", NumberOfReads = 3338 },
                    };
    }
}

//索引.cshtml

All Blogs List
@Html.Action("myblogs")

<br />
<br />

Popular Tutorial
@Html.Action("mypopular","thepopular")

//MyPopular.cshtml

@model IEnumerable<MyArticleSummaryTEST.PopularTutorial>

@{
var grid = new WebGrid(Model, canPage: true, canSort: false, rowsPerPage: 3);
}

@grid.GetHtml(
            columns: grid.Columns(grid.Column("", format: @<text>@item.Title</text>))
        )


@using (Html.BeginForm())
{
<div>
    <input type="submit" name ="popularpress" id="2"/>  
</div>
}

//MyBlogs.cshtml

@model IEnumerable<MyArticleSummaryTEST.Blog>

<section>
<ul>
    @Html.DisplayForModel()
</ul>
</section>

@using (Html.BeginForm())
{
<div>
<input type="submit" name ="blogclick" id="1"/>  
</div>
}

//博客展示模板

@model MyArticleSummaryTEST.Blog

<li>
<h3>@Html.DisplayFor(x => x.Head)</h3>
@Html.DisplayFor(x => x.Content)
</li>

阅读:

  1. asp.net MVC partial view controller action

  2. Using Html.BeginForm to post to the current controller

  3. Loading a partial view in jquery.dialog

  4. How can i generate html in action from partial view?

  5. Returning Redirect or PartialView from the same Action

  6. Redirect to Refer on Partial View Form Post using ASP.NET MVC

  7. Why are Redirect Results not allowed in Child Actions in Asp.net MVC 2

  8. ValidationSummary not appearing with Partial Views

  9. 在 ASP.Net MVC 2 中以“正确”方式从部分视图重定向 http://geekswithblogs.net/DougLampe/archive/2011/08/05/redirecting-from-a-partial-view-the-right-way-in-asp.net.aspx

  10. ASP.NET MVC 中的部分请求 http://blog.stevensanderson.com/2008/10/14/partial-requests-in-aspnet-mvc/

  11. asp.net mvc 3 和 jquery 的渐进增强教程 http://www.matthidinger.com/archive/2011/02/22/Progressive-enhancement-tutorial-with-ASP-NET-MVC-3-and-jQuery.aspx

【问题讨论】:

  • 你有一些基本的误解。在您的 index.cshtml 中,您希望使用 ActionLink,而不是 Action。 Action 将尝试执行该操作,在这种情况下是不合适的 HTTP 重定向,而不是提供您可以单击的链接。我确定这是您想要的链接。鉴于不需要 ChildActionOnly - 它会阻止您想要的 - 或 POST 操作,因为它将是一个 GET 请求。我可能对你想要达到的目标有误,但我认为你让它变得比它需要的更复杂。

标签: c# asp.net .net asp.net-mvc asp.net-mvc-3


【解决方案1】:

您的代码中有 number 个错误:

  1. 当从父操作 Index 调用子操作 MyBlogs 时,在 MyBlogs 视图中具有 @using (Html.BeginForm()),会生成发布到 Index 操作的表单,不是 MyBlogs 一个 . Populars 的情况相同。因此,毫不奇怪,每次提交都会重新呈现索引操作内容 - 这是您的表单请求的操作。尝试使用接受路由参数的 Html.BeginForm 的重载。
  2. [ChildActionOnly] 表示外部世界无法访问该操作,可以请求 HttpGet、Post、通过 url 或任何其他方式。它只能与Html.Action 助手一起使用。因此,当您更正第一个错误时,您仍然无法发布该操作。如果该操作应处理发布请求,则应删除 ChildActionOnly 属性。
  3. 如果是您发布的真实代码,它不会(也不应该)重定向。您应该更正方法签名并添加缺少的 return 语句

这段代码

[HttpPost]
public void MyBlogs(string blogclick)
{
    RedirectToAction("BlogHome");
}

应该是

[HttpPost]
public ActionResult MyBlogs(string blogclick)
{
    return RedirectToAction("BlogHome");
}

这应该可以工作

【讨论】:

  • 谢谢。我更正了上面提到的三个项目。现在,我收到“执行处理程序'System.Web.Mvc.HttpHandlerUtil+ServerExecuteHttpHandlerAsyncWrapper'的子请求时出错”。从控制器返回后,此错误发生在 Index.cshtml 中。我所做的更改类似于 MyBlogs.cshtml 和 MyPopular.cshtml 中的 Html.BeginForm("Index","Article")。我们如何让它发挥作用?
  • @Lijo 内部异常怎么说?
  • 内部异常说,“不允许子动作执行重定向动作。”}。但是我已经从我编写的代码中删除了所有“孩子”这个词。它仍然抱怨孩子的行为。我们如何纠正它?
  • 如果您发布到索引操作,当使用@Html.Action 渲染部分视图时,将选择 HttpPost MyBlogs,而不是 HttpGet 之一。 HttpPost MyBlogs 执行重定向,所以你得到一个错误
【解决方案2】:

问题在于@using (Html.BeginForm())

当我们在 BeginForm 中没有指定任何 action 和控制器名称时,它会将 Data 发布到页面的 Url。 (在这种情况下文章/索引)。

你可以指定Action和Controller来发布数据,

例如,在 MyBlogs.cshtml 中

@using(Html.BeginForm("MyBlogs","Article")){
...}

在 MyPopular.cshtml 中

@using(Html.BeginForm("MyPopular","ThePopular")){
...}

【讨论】:

  • 单击按钮时出现以下错误页面。我们如何纠正它? “在编译服务此请求所需的资源期间发生错误。命名空间“MyArticleSummaryTEST”中不存在类型或命名空间名称“ArticleSummary”(您是否缺少程序集引用?)“”公共类_Page_Views_ThePopular_PoularHome_cshtml:系统。 Web.Mvc.WebViewPage { "
  • 只需验证 ArticleSummary 类是否属于 MyArticleSummaryTEST 命名空间......
【解决方案3】:

嗯,不确定这是否是完整的故事,但你有:

public string PoularHome()
{
    return "Poular Home";
}

这只是一种方法。然后你发出(来自你的MyPopular 方法):

RedirectToAction("PoularHome");

由于PoularHome() 没有返回ActionResult 的类型(或派生),因此管道将忽略此“请求”。您需要认真考虑返回适当的类型。尝试重构您的方法(操作),看看它是否有帮助:

public ContentResult PoularHome()
{
    return Content("Poular Home");
}

没有保证 - 只是 30K 英尺的观察。

【讨论】:

  • 当我使用 ContentResult 甚至 ActionResult(并返回 View() objetc)时不起作用。我的简单问题是——我们通常如何从部分视图重定向到其他视图?
【解决方案4】:

根据您的代码,您似乎正在重定向到当前控制器中不存在的操作。如果动作不在当前控制器中,您需要使用指定控制器和动作的重载。否则它只会掉入默认路由并将您发送到 index.html。

您还需要返回一个 RedirectToAction。这仍然是一种必须返回的 ActionResult。

【讨论】:

    猜你喜欢
    • 2012-03-12
    • 1970-01-01
    • 1970-01-01
    • 2012-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多