ASP.NET MVC3 RAZOR:从部分视图重定向

发布于 2025-01-08 05:18:10 字数 6226 浏览 0 评论 0原文

我有两个部分视图“MyPopular”和“MyBlogs”。有两个控制器——“ArticleController.cs”和“ThePopularController.cs”。这两个局部视图都包含按钮。

最初,它在索引视图内呈现两个部分视图。

在博客点击的后操作处理程序上,要求重定向到“BlogHome”操作,其中它将返回一个简单的字符串“Blog Home”(而不是视图)。在流行点击的后操作处理程序上,要求重定向到“PopularHome”操作,其中它将返回一个简单的字符串“Popular Home”。但目前,当我单击任何按钮时,它都会呈现 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>

阅读:

  1. asp.net MVC 部分视图控制器操作

  2. 使用 Html.BeginForm 发布到当前控制器

  3. 加载部分在 jquery.dialog 中查看

  4. 如何我从部分视图中生成 html?

  5. 从同一操作返回重定向或 PartialView

  6. 使用 ASP.NET MVC 重定向至部分视图表单发布

  7. 为什么不允许重定向结果在 Asp.net MVC 2 中的子操作

  8. ValidationSummary 未与部分视图一起出现

  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

I have two partial views “MyPopular” and “MyBlogs”. And there are two controllers – “ArticleController.cs” and “ThePopularController.cs”. Both these partialviews contains buttons.

Initially it renders both partial views inside the index view.

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.

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.

How do we correct it to do the proper redirecting?

//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>
}

//Blog Display Template

@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. Redirecting from a Partial View the "Right" Way in 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. Partial Requests in ASP.NET MVC
    http://blog.stevensanderson.com/2008/10/14/partial-requests-in-aspnet-mvc/

  11. Progressive enhancement tutorial with asp.net mvc 3 and jquery
    http://www.matthidinger.com/archive/2011/02/22/Progressive-enhancement-tutorial-with-ASP-NET-MVC-3-and-jQuery.aspx

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

相守太难 2025-01-15 05:18:10

您的代码中有数量个错误:

  1. 从父操作 Index 调用子操作 MyBlogs 时,有 @using (Html.BeginForm ()) 在 MyBlogs 视图中,生成发布到 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");
}

这应该可以工作

There are number of errors in you code:

  1. When calling child action MyBlogs from parent action Index, having @using (Html.BeginForm()) in the MyBlogs view, generates the form that posts to Index action, not the MyBlogs one. Same story for Populars. So, no suprise that every submit re-renders Index action contents - that is action requested by your form. Try using overload of Html.BeginForm that accepts route parameters.
  2. [ChildActionOnly] means that action is not accessible by outside world, be request HttpGet, Post, by url or any other means. It can be used only with Html.Action helper. So, when you correct the 1st error, you won't be still able to post on that action. You should remove ChildActionOnly attribute if that action should handle post requests.
  3. If it's the real code you posted, it does not (and should not) redirect. You should correct method signature and add missing return statement

This code

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

Should be

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

This should work

熟人话多 2025-01-15 05:18:10

问题出在 @using (Html.BeginForm()) 上。

当我们在 BeginForm 中没有指定任何操作和控制器名称时,它会将数据发布到页面的 Url 中。 (在这种情况下文章/索引)。

您可以指定 Action 和 Controller 来发布数据,

例如,在 MyBlogs.cshtml 中

@using(Html.BeginForm("MyBlogs","Article")){
...}

的 MyPopular.cshtml 中

@using(Html.BeginForm("MyPopular","ThePopular")){
...}

The problem lies with @using (Html.BeginForm()).

When we do not specify any action and controller name in BeginForm, It will post Data to the Url of the page. (In this scenario Article/Index ).

You can specify the Action and Controller to post the data,

Like, in MyBlogs.cshtml

@using(Html.BeginForm("MyBlogs","Article")){
...}

in MyPopular.cshtml

@using(Html.BeginForm("MyPopular","ThePopular")){
...}
活泼老夫 2025-01-15 05:18:10

好吧,不确定这是否是完整的故事,但你知道:

public string PoularHome()
{
    return "Poular Home";
}

这只是一种方法。然后你发出(从你的MyPopular方法):

RedirectToAction("PoularHome");

由于PoularHome()没有返回ActionResult类型(或派生),那么管道将忽略这个“请求”。您需要认真考虑返回适当的类型。尝试重构您的方法(操作),看看它是否有帮助:

public ContentResult PoularHome()
{
    return Content("Poular Home");
}

没有保证 - 只是 30K 英尺的观察。

well, not sure if it's the full story or not, but you have:

public string PoularHome()
{
    return "Poular Home";
}

which is merely a method. you then issue (from your MyPopular method):

RedirectToAction("PoularHome");

As PoularHome() isn't returning a type of ActionResult (or derivation), then the pipeline is going to ignore this 'request'. You need to seriously look at returning the appropriate type. Try refactoring your method (action) as such and see if it helps:

public ContentResult PoularHome()
{
    return Content("Poular Home");
}

no guarantees - just a 30K feet observation.

征﹌骨岁月お 2025-01-15 05:18:10

根据您的代码,您似乎正在重定向到不在当前控制器中的操作。如果操作不在当前控制器中,则需要使用指定控制器和操作的重载。否则它只会进入默认路由并将您发送到index.html。

您还需要返回一个 RedirectToAction。这仍然是一种必须返回的 ActionResult 类型。

By your code it looks like you are redirecting to an action that isn't in the current controller. If the action isn't in the current controller you need to use the overload that specifies both the controller and the action. Otherwise it will just drop into the default route and send you to index.

Also you need to Return a RedirectToAction. This is still a type of ActionResult that must be returned.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文