2012-02-23 47 views
3

我有两个部分视图“MyPopular”和“MyBlogs”。有两个控制器 - “ArticleController.cs”和“ThePopularController.cs”。这两个部分视图都包含按钮。ASP.NET MVC3 RAZOR:从部分视图重定向

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

在博客点击的发布操作处理程序中,它被要求重定向到“BlogHome”操作,它将返回一个简单字符串“Blog Home”(而不是视图)。在流行的点击后处理处理程序上,它会被要求重定向到“PopularHome”操作,它会返回一个简单的字符串“流行家园”。但目前,当我点击任何按钮时,它呈现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 ..." } 
        }; 
    } 



} 

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

READING:

  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

+1

在MyBlogs.cshtml

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

你有一些根本性的误解。在你的index.cshtml中,你想使用ActionLink,而不是Action。 Action会尝试执行该操作,在这种情况下,HTTP重定向不合适,而不是提供您可以点击的链接。我确定这是一个你想要的链接。考虑到不需要ChildActionOnly - 它会阻止你想要的 - 或者POST动作,因为它会是一个GET请求。我可能对你想要达到的目标有所错,但我认为你已经使它变得比需要的复杂。 – tvanfosson 2012-02-24 14:13:49

回答

1

好,不知道这是否是完整的故事或没有,但你必须:

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

这仅仅是一种方法。你则(从您MyPopular法)发布:

RedirectToAction("PoularHome"); 

由于PoularHome()没有返回一个类型的ActionResult(或派生),则管道会忽略这个“请求”。你需要认真看待返回适当的类型。尝试重构你的方法(动作),因此,看看是否有帮助:

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

没有保障 - 只有30K英尺观察。

+0

当我使用ContentResult甚至ActionResult(并返回一个View()objetc)时没有工作。我的简单问题是 - 我们通常如何通过部分视图重定向到其他视图? – Lijo 2012-02-23 13:54:40

4

有在你的代码的错误

  1. 当调用从父行动Index子操作MyBlogs,其在MyBlogs @using (Html.BeginForm())查看,生成形式职位,以Index行动,MyBlogs one。同样的故事Populars。因此,不要惊讶每次提交都会重新呈现索引操作内容 - 这是您的表单所要求的操作。尝试使用接受路由参数的Html.BeginForm的重载。
  2. [ChildActionOnly]意味着动作不会被外界访问,是请求HTTPGET,邮政,通过网址或其他任何方式。它只能与Html.Action助手一起使用。所以,当您更正第一个错误时,您将无法继续发布该操作。如果该操作应处理发布请求,则应删除ChildActionOnly属性。
  3. 如果这是您发布真正的代码,它没有(也不应该)重定向。你应该正确的方法签名,并添加缺少的return声明

此代码

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

应该

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

这应该工作

+0

谢谢。我纠正了上述三个项目。现在,我得到“错误执行处理程序的子请求'System.Web.Mvc.HttpHandlerUtil + ServerExecuteHttpHandlerAsyncWrapper'”。从控制器返回后,Index.cshtml中发生此错误。我所做的更改与MyBlogs.cshtml和MyPopular.cshtml中的Html.BeginForm(“索引”,“文章”)相似。我们如何使它工作? – Lijo 2012-02-23 15:38:37

+0

@Lijo内部例外说什么? – archil 2012-02-23 16:13:31

+0

内部异常说:“不允许子操作执行重定向操作。”}。但是我已经从我编写的代码中删除了所有“孩子”字样。它仍然抱怨孩子的行为。我们如何纠正它? – Lijo 2012-02-24 05:22:23

0

通过您的代码,它看起来像要重定向转换为不在当前控制器中的操作。如果操作不在当前控制器中,则需要使用指定控制器和操作的重载。否则,它将放入默认路由并发送给索引。

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

3

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

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

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

一样,在MyPopular.cshtml

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

当点击按钮时,我收到以下错误页面。我们如何纠正它? “在编译为请求提供服务所需的资源时发生错误,名称空间”MyArticleSummaryTEST“中没有类型或名称空间名称”ArticleSummary“(缺少程序集引用?)”“public class _Page_Views_ThePopular_PoularHome_cshtml:System。 Web.Mvc.WebViewPage { “ – Lijo 2012-02-24 05:27:06

+0

只需验证ArticleSummary类是否属于MyArticleSummaryTEST名称空间.... – Manas 2012-02-24 06:37:16