Note that there are some explanatory texts on larger screens.

plurals
  1. POASP.NET MVC3 RAZOR: Redirecting from Partial Views
    primarykey
    data
    text
    <p>I have two partial views “MyPopular” and “MyBlogs”. And there are two controllers – “ArticleController.cs” and “ThePopularController.cs”. Both these partialviews contains buttons. </p> <p>Initially it renders both partial views inside the index view.</p> <p>On post action handler for blog’s click, it is asked to redirect to “BlogHome“ action where it will return a simple string “Blog Home” (instead of a view). On post action handler for popular’s click, it is asked to redirect to “PopularHome“ action where it will return a simple string “Popular Home”. But currently, when I click on any of the button, it renders localhost:1988/Article index; without partial content.</p> <p>Note: The result is same even when I used ContentResult and ActionResult. Note: Please highlight if I am going through the wrong way for achieving such a simple task.</p> <p>How do we correct it to do the proper redirecting?</p> <p>//ArticleController</p> <pre><code>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&lt;Blog&gt; 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 ..." } }; } } </code></pre> <p>// ThePopularController</p> <pre><code>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&lt;PopularTutorial&gt; 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 }, }; } } </code></pre> <p>//Index.cshtml</p> <pre><code>All Blogs List @Html.Action("myblogs") &lt;br /&gt; &lt;br /&gt; Popular Tutorial @Html.Action("mypopular","thepopular") </code></pre> <p>//MyPopular.cshtml</p> <pre><code>@model IEnumerable&lt;MyArticleSummaryTEST.PopularTutorial&gt; @{ var grid = new WebGrid(Model, canPage: true, canSort: false, rowsPerPage: 3); } @grid.GetHtml( columns: grid.Columns(grid.Column("", format: @&lt;text&gt;@item.Title&lt;/text&gt;)) ) @using (Html.BeginForm()) { &lt;div&gt; &lt;input type="submit" name ="popularpress" id="2"/&gt; &lt;/div&gt; } </code></pre> <p>//MyBlogs.cshtml</p> <pre><code>@model IEnumerable&lt;MyArticleSummaryTEST.Blog&gt; &lt;section&gt; &lt;ul&gt; @Html.DisplayForModel() &lt;/ul&gt; &lt;/section&gt; @using (Html.BeginForm()) { &lt;div&gt; &lt;input type="submit" name ="blogclick" id="1"/&gt; &lt;/div&gt; } </code></pre> <p>//Blog Display Template</p> <pre><code>@model MyArticleSummaryTEST.Blog &lt;li&gt; &lt;h3&gt;@Html.DisplayFor(x =&gt; x.Head)&lt;/h3&gt; @Html.DisplayFor(x =&gt; x.Content) &lt;/li&gt; </code></pre> <p>READING:</p> <ol> <li><p><a href="https://stackoverflow.com/questions/1371031/asp-net-mvc-partial-view-controller-action">asp.net MVC partial view controller action</a></p></li> <li><p><a href="https://stackoverflow.com/questions/1031452/using-html-beginform-to-post-to-the-current-controller">Using Html.BeginForm to post to the current controller</a></p></li> <li><p><a href="https://stackoverflow.com/questions/4802378/loading-a-partial-view-in-jquery-dialog">Loading a partial view in jquery.dialog</a></p></li> <li><p><a href="https://stackoverflow.com/questions/8476596/how-can-i-generate-html-in-action-from-partial-view">How can i generate html in action from partial view?</a></p></li> <li><p><a href="https://stackoverflow.com/questions/4184700/returning-redirect-or-partialview-from-the-same-action">Returning Redirect or PartialView from the same Action</a></p></li> <li><p><a href="https://stackoverflow.com/questions/1064542/redirect-to-refer-on-partial-view-form-post-using-asp-net-mvc">Redirect to Refer on Partial View Form Post using ASP.NET MVC</a></p></li> <li><p><a href="https://stackoverflow.com/questions/2056421/why-are-redirect-results-not-allowed-in-child-actions-in-asp-net-mvc-2">Why are Redirect Results not allowed in Child Actions in Asp.net MVC 2</a></p></li> <li><p><a href="https://stackoverflow.com/questions/8986634/validationsummary-not-appearing-with-partial-views">ValidationSummary not appearing with Partial Views</a></p></li> <li><p>Redirecting from a Partial View the "Right" Way in ASP.Net MVC 2 <a href="http://geekswithblogs.net/DougLampe/archive/2011/08/05/redirecting-from-a-partial-view-the-right-way-in-asp.net.aspx" rel="nofollow noreferrer">http://geekswithblogs.net/DougLampe/archive/2011/08/05/redirecting-from-a-partial-view-the-right-way-in-asp.net.aspx</a></p></li> <li><p>Partial Requests in ASP.NET MVC <a href="http://blog.stevensanderson.com/2008/10/14/partial-requests-in-aspnet-mvc/" rel="nofollow noreferrer">http://blog.stevensanderson.com/2008/10/14/partial-requests-in-aspnet-mvc/</a></p></li> <li><p>Progressive enhancement tutorial with asp.net mvc 3 and jquery <a href="http://www.matthidinger.com/archive/2011/02/22/Progressive-enhancement-tutorial-with-ASP-NET-MVC-3-and-jQuery.aspx" rel="nofollow noreferrer">http://www.matthidinger.com/archive/2011/02/22/Progressive-enhancement-tutorial-with-ASP-NET-MVC-3-and-jQuery.aspx</a></p></li> </ol>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload