gpt4 book ai didi

c# - ASP.NET MVC3 Razor : Redirecting from Partial Views

转载 作者:行者123 更新时间:2023-11-30 14:00:59 26 4
gpt4 key购买 nike

我有两个部分 View “MyPopular”和“MyBlogs”。并且有两个 Controller ——“ArticleController.cs”和“ThePopularController.cs”。这两个局部 View 都包含按钮。

最初它在索引 View 中呈现两个部分 View 。

在博客点击的后处理程序中,它被要求重定向到“BlogHome”操作,它将返回一个简单的字符串“Blog Home”(而不是 View )。在流行的点击后 Action 处理程序中,它被要求重定向到“PopularHome” Action ,它将返回一个简单的字符串“Popular Home”。但目前,当我点击任何按钮时,它会呈现 localhost:1988/Article index;没有部分内容。

注意:即使我使用了 ContentResult 和 ActionResult,结果也是一样的。注意:请突出显示我是否通过错误的方式来完成如此简单的任务。

我们如何更正它以进行正确的重定向?

//文章 Controller

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 },
};
}
}

//索引.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>

阅读:

  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 中以“正确”的方式从局部 View 重定向 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. 当从父操作 Index 调用子操作 MyBlogs 时,在 MyBlogs View 中使用 @using (Html.BeginForm()),生成发布到 Index 操作的表单,不是 MyBlogs 一个Populars 同样的故事。因此,毫不奇怪,每次提交都会重新呈现索引操作内容——这是您的表单请求的操作。尝试使用接受路由参数的 Html.BeginForm 重载。
  2. [ChildActionOnly] 表示外部世界无法访问操作,请求 HttpGet、Post、url 或任何其他方式。它只能与 Html.Action 助手一起使用。因此,当您更正第一个错误时,您仍然无法发布该操作。如果该操作应该处理发布请求,您应该删除 ChildActionOnly 属性。
  3. 如果这是您发布的真实代码,它不会(也不应该)重定向。您应该更正方法签名并添加缺少的 return 语句

这段代码

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

应该是

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

这应该可行

关于c# - ASP.NET MVC3 Razor : Redirecting from Partial Views,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9413467/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com