我有两个部分的意见MyPopular和MyBlogs。并有两个控制器 - ArticleController.cs和ThePopularController.cs。这两个partialviews包含按钮。
起初,它呈现指数视图内两个部分景色。
在后期操作处理程序博客的点击,它被要求重定向到BlogHome行动在那里它会返回一个简单的字符串博客之家(而不是一个视图)。在后期操作处理程序流行的点击,它被要求重定向到PopularHome行动在那里它会返回一个简单的字符串流行的家庭。但是目前,当我点击任何按钮,这使得本地主机:1988 /条指数;没有部分内容。
注:结果是一样的,即使我用ContentResult类型和的ActionResult。 注:请突出,如果我经历了错误的方式实现这样一个简单的任务。
我们如何纠正它做了正确的重定向?
// ArticleController
公共类ArticleController:控制器
{
公众的ActionResult指数()
{
//指数收益没有模型
返回查看();
}
公共字符串BlogHome()
{
返回博客主页;
}
// ChildActionOnly属性表明,这一行动不应通过URL来直接调用。
[ChildActionOnly]
公众的ActionResult MyBlogs()
{
Thread.sleep代码(500);
返回PartialView(GetAllBlogEntries());
}
[ChildActionOnly]
[HttpPost]
公共无效MyBlogs(字符串blogclick)
{
RedirectToAction(BlogHome);
}
私人的IEnumerable<博客> GetAllBlogEntries()
{
返回新的[]
{
新的博客{n = 1,头=介绍MVC,PostBy =Lijo,内容=这是一个......},
新的博客{n = 2,头=jQuery的隐藏的宝石,PostBy =Lijo,内容=这是一个......},
新的博客{n = 3,头=Web表单Intenals,PostBy =Lijo,内容=这是一个......}
};
}
}
// ThePopularController
公共类ThePopularController:控制器
{
公共字符串PoularHome()
{
返回杨树品种家;
}
// ChildActionOnly属性表明,这一行动不应通过URL来直接调用。
[ChildActionOnly]
公众的ActionResult MyPopular()
{
Thread.sleep代码(500);
返回PartialView(GetPopularBlogs());
}
[ChildActionOnly]
[HttpPost]
公共无效MyPopular(字符串流行的preSS)
{
RedirectToAction(PoularHome);
}
私人的IEnumerable< PopularTutorial> GetPopularBlogs()
{
返回新的[]
{
新PopularTutorial {n = 17,标题=测试1,NumberOfReads = 1050},
新PopularTutorial {n = 18,标题=的Test2,NumberOfReads = 5550},
新PopularTutorial {n = 19,标题=Test3的,NumberOfReads = 3338},
新PopularTutorial {n = 20,标题=TEST4,NumberOfReads = 3338},
新PopularTutorial {n = 21,标题=TEST5,NumberOfReads = 3338},
新PopularTutorial {n = 22,标题=TEST6,NumberOfReads = 3338},
新PopularTutorial {n = 23,标题=TEST7,NumberOfReads = 3338},
};
}
}
// Index.cshtml
所有博客列表
@ Html.Action(myblogs)
< BR />
< BR />
热门文章
@ Html.Action(mypopular,thepopular)
// MyPopular.cshtml
@model IEnumerable的< MyArticleSummaryTEST.PopularTutorial>
@ {
VAR电网=新的WebGrid(型号,canPage:真,canSort:假的,rowsPerPage:3);
}
@ grid.GetHtml(
列:grid.Columns(grid.Column(,格式为:@<文本> @ item.Title< /文>))
)
@using(Html.BeginForm())
{
< DIV>
<输入类型=提交名称=最受欢迎preSSID =2/>
< / DIV>
}
// MyBlogs.cshtml
@model IEnumerable的< MyArticleSummaryTEST.Blog>
&其中;节>
< UL>
@ Html.DisplayForModel()
< / UL>
< /节>
@using(Html.BeginForm())
{
< DIV>
<输入类型=提交名称=blogclickID =1/>
< / DIV>
}
//博客显示模板
@model MyArticleSummaryTEST.Blog
<李>
< H3> @ Html.DisplayFor(X => x.Head)< / H3>
@ Html.DisplayFor(X => x.Content)
< /李>
阅读:
asp.net MVC局部视图控制器操作
Using Html.BeginForm张贴到电流控制器
加载在jquery.dialog 的局部视图
How我可以生成html在行动,从局部来看?
Returning从相同的操作重定向或PartialView
Redirect使用ASP.NET MVC 偏查看表单提交指
Why被重定向结果未在Asp.net MVC 2 儿童的行动使
ValidationSummary没有出现与局部景观
从一个局部视图重定向正确的方式在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
解决方案有号在你code错误的:
当调用子操作 MyBlogs
从家长行动首页
,有 @using(HTML .BeginForm())
在MyBlogs视图,生成形式职位首页
操作,不是 MyBlogs
一个。同样的故事 Populars
。因此,没有惊喜,每一个提交重新呈现指数作用的内容 - 这是你的形式要求的动作。尝试使用Html.BeginForm接受路由参数的过载。
[ChildActionOnly]
意味着动作不会被外界访问,是要求HTTPGET,邮政,通过网址或其他任何方式。它只能用于与 Html.Action
帮手。所以,当你纠正错误一日,你会不会依然能够张贴在行动。您应该删除 ChildActionOnly
属性,如果该操作应处理POST请求。
如果是c您发布真正的$ C $,它没有(也不应该)重定向。你应该正确的方法签名,并添加缺少的返回
语句
这code
[HttpPost]
公共无效MyBlogs(字符串blogclick)
{
RedirectToAction(BlogHome);
}
应
[HttpPost]
公众的ActionResult MyBlogs(字符串blogclick)
{
返回RedirectToAction(BlogHome);
}
这应该工作
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:
asp.net MVC partial view controller action
Using Html.BeginForm to post to the current controller
Loading a partial view in jquery.dialog
How can i generate html in action from partial view?
Returning Redirect or PartialView from the same Action
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
ValidationSummary not appearing with Partial Views
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
Partial Requests in ASP.NET MVC http://blog.stevensanderson.com/2008/10/14/partial-requests-in-aspnet-mvc/
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
解决方案
There are number of errors in you code:
When calling child actionMyBlogs
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.
[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.
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
相关推荐
最新文章