2012-02-23 46 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