ASP.NET MVC3 RAZOR:从部分视图重定向

3
我有两个部分视图“MyPopular”和“MyBlogs”,还有两个控制器:“ArticleController.cs”和“ThePopularController.cs”。这两个部分视图都包含按钮。
最初,它会在index视图中呈现这两个部分视图。
在博客点击的post操作处理程序中,要求重定向到“BlogHome”操作,其中它将返回一个简单的字符串“Blog Home”(而不是视图)。在流行点击的post操作处理程序中,要求重定向到“PopularHome”操作,其中它将返回一个简单的字符串“Popular Home”。但目前,当我点击任何一个按钮时,它会呈现localhost:1988/Article index;没有部分内容。
注意:即使使用ContentResult和ActionResult,结果也是相同的。 注意:请指出是否我正在实现这样一个简单任务的错误方式。
我们如何更正它以进行正确的重定向?
// ArticleController
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 ..." }
                    };
    }



}

// 这是PopularController

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 },
                    };
    }
}

//Index.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部分视图控制器操作

  2. 使用Html.BeginForm提交到当前控制器

  3. 在jquery.dialog中加载部分视图

  4. 如何从部分视图中的操作生成HTML?

  5. 从同一操作返回重定向或部分视图

  6. 使用ASP.NET MVC在部分视图表单提交后重定向到Refer

  7. 为什么在Asp.net MVC 2中不允许在子操作中使用重定向结果

  8. 部分视图中ValidationSummary不显示

  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


1
你有一些基本的误解。在你的index.cshtml中,你应该使用ActionLink而不是Action。Action会尝试执行操作,这种情况下是一个不合适的HTTP重定向,而不是提供一个可以点击的链接。我相信你想要的是一个链接。鉴于没有必要使用ChildActionOnly - 它会阻止你想要的结果 - 或者POST操作,因为它将是一个GET请求。我可能对你想要实现的内容有所误解,但我认为你把它搞得比必要的更复杂了。 - tvanfosson
4个回答

4

你的代码中有number个错误:

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

这是代码:

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

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

这应该可以工作


谢谢。我已经纠正了上述三个项目。现在,当从控制器返回后,在Index.cshtml中出现“执行子请求时出错,处理程序为'System.Web.Mvc.HttpHandlerUtil+ServerExecuteHttpHandlerAsyncWrapper'”。我所做的更改是在MyBlogs.cshtml和MyPopular.cshtml中使用Html.BeginForm("Index","Article")。我们该如何使其工作? - LCJ
@Lijo 内部异常信息是什么? - archil
内部异常显示:“不允许子操作执行重定向操作。”}。但是我已经从我编写的代码中删除了所有“子”字。它仍然抱怨子操作。我们该如何纠正这个问题? - LCJ
如果您在Post到Index操作时,使用@Html.Action渲染局部视图,则会选择HttpPost MyBlogs而不是HttpGet。 HttpPost MyBlogs执行重定向,因此您会收到错误。 - archil

3
问题出在@using (Html.BeginForm())上。
如果我们在BeginForm中没有指定任何操作和控制器名称,则会将数据发布到页面的URL上(在此场景中为Article/Index)。
您可以指定要发布数据的Action和Controller,
例如,在MyBlogs.cshtml中。
@using(Html.BeginForm("MyBlogs","Article")){
...}

在MyPopular.cshtml中

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

当我点击按钮时,出现以下错误页面。我们该如何纠正它?“在编译为服务此请求所需的资源时发生错误。名称空间'MyArticleSummaryTEST'中不存在类型或命名空间名称'ArticleSummary'(是否缺少程序集引用?)”“public class _Page_Views_ThePopular_PoularHome_cshtml : System.Web.Mvc.WebViewPage<MyArticleSummaryTEST.ArticleSummary> {” - LCJ
只需验证ArticleSummary类是否属于MyArticleSummaryTEST命名空间... - Manas

1

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

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

这只是一种方法。然后你从你的MyPopular方法中发出:

RedirectToAction("PoularHome");

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

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

无保证 - 只是一个30,000英尺的观察。


当我使用ContentResult或者ActionResult(并且返回了一个View()对象)时都没有起作用。我的简单问题是 - 我们通常如何从局部视图重定向到其他视图? - LCJ

0
根据您的代码,看起来您正在重定向到当前控制器中不存在的操作。如果该操作不在当前控制器中,则需要使用指定控制器和操作的重载。否则,它将跳转到默认路由并发送您到索引页面。
另外,您需要返回一个RedirectToAction。这仍然是必须返回的ActionResult类型之一。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接