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

时间:2012-02-23 12:53:06

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

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

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

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

注意:即使我使用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 ..." }
                    };
    }



}

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

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

4 个答案:

答案 0 :(得分:4)

您的代码中有数量错误:

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

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

    应该是

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

    这应该有效

答案 1 :(得分:3)

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

如果我们在BeginForm中未指定任何操作和控制器名称,它会将数据发布到页面的URL。 (在此场景文章/索引中)。

您可以指定动作和控制器来发布数据

喜欢,在MyBlogs.cshtml中

@using(Html.BeginForm("MyBlogs","Article")){
...}
MyPopular.cshtml中的

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

答案 2 :(得分:1)

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

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

这仅仅是一种方法。然后发出(从您的MyPopular方法):

RedirectToAction("PoularHome");

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

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

无保证 - 只需30K英尺的观察。

答案 3 :(得分:0)

通过您的代码,您似乎正在重定向到不在当前控制器中的操作。如果操作不在当前控制器中,则需要使用指定控制器和操作的重载。否则它只会进入默认路由并将您发送到索引。

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

相关问题