我有两个部分视图“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:
Redirect to Refer on Partial View Form Post using ASP.NET MVC
Why are Redirect Results not allowed in Child Actions in Asp.net MVC 2
在ASP.Net MVC 2个 http://geekswithblogs.net/DougLampe/archive/2011/08/05/redirecting-from-a-partial-view-the-right-way-in-asp.net.aspx
在ASP部分请求从局部视图重定向的 “正确” 的方式。NET MVC http://blog.stevensanderson.com/2008/10/14/partial-requests-in-aspnet-mvc/
与asp.net的MVC 3和jQuery渐进增强教程 http://www.matthidinger.com/archive/2011/02/22/Progressive-enhancement-tutorial-with-ASP-NET-MVC-3-and-jQuery.aspx
在MyBlogs.cshtml
你有一些根本性的误解。在你的index.cshtml中,你想使用ActionLink,而不是Action。 Action会尝试执行该操作,在这种情况下,HTTP重定向不合适,而不是提供您可以点击的链接。我确定这是一个你想要的链接。考虑到不需要ChildActionOnly - 它会阻止你想要的 - 或者POST动作,因为它会是一个GET请求。我可能对你想要达到的目标有所错,但我认为你已经使它变得比需要的复杂。 – tvanfosson 2012-02-24 14:13:49