我有兩個部分視圖「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