Asp.Net Mvc 2 - 带有创建的 RenderAction 列表
首先,我使用 Asp.Net MVC 2 RC 2。
我想要做的是列出一个评论视图,并在该视图下方能够添加评论(带验证)。例如,当您在 stackoverflow 中添加评论时。除了我的页面应该在启用或不启用 JavaScript 的情况下工作。
因此,为了解决这个问题,我使用了新的 RenderAction,它部分解决了我的问题。我的列表视图使用 RenderAction 调用我的 addcomment 用户控件。
验证有效。当我尝试添加有效的评论时,就会出现问题。页面未正确刷新。如果我进入数据库,我的评论会被添加,但它在我的列表视图中没有刷新,并且添加评论表单不清楚。
我认为这是因为工作流程的呈现方式。
也许如果有人有关于此的示例或博客,它可以帮助我正确处理...
在我的 Comment/List.aspx 底部
<% Html.RenderAction("Create", "Comment"); %>
In Comment/Create.ascx
<% using (Html.BeginForm(
ViewContext.ParentActionViewContext.RouteData
.Values["action"].ToString(),
ViewContext.ParentActionViewContext.RouteData
.Values["controller"].ToString(),
FormMethod.Post, new { id = "createForm" })){ %>
First, I use Asp.Net MVC 2 RC 2.
What I want to do is to list a comment view and below this view being able to add comment (with validations). For example, something like when you add comment in stackoverflow. Except that my page should work with or without javascript enabled.
So to solve that problem I use the new RenderAction and it partially solved my problem. I got my list view that calls my addcomment usercontrol with RenderAction.
The validations work. My problem occurs when I try to add a comment that it's valid. The page didn't refresh correctly. If I got in the database, my comment is added, but it's doesn't have been refresh in my list view and the add comment form isn't clear.
I think it's because of how the workflow is rendered.
Maybe if someone got a example or blog about this, it's can help me to get it right...
At the bottom of my Comment/List.aspx
<% Html.RenderAction("Create", "Comment"); %>
In Comment/Create.ascx
<% using (Html.BeginForm(
ViewContext.ParentActionViewContext.RouteData
.Values["action"].ToString(),
ViewContext.ParentActionViewContext.RouteData
.Values["controller"].ToString(),
FormMethod.Post, new { id = "createForm" })){ %>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以通过涉及 ViewContext.ParentActionViewContext 的小技巧强制父视图刷新自身。
在您的 CommentController 类中:
在您的 Comment/List.aspx 页面(视图)中:
所以基本上,发生的情况是子操作“告诉”父操作通过使用父操作的 ViewData 来刷新自身。
这有点像黑客,但对于你正在做的事情来说它工作得很好。
You can force the parent View to refresh itself with a small hack involving ViewContext.ParentActionViewContext.
In your CommentController class:
And in your Comment/List.aspx page (view) :
So basically, what's happening is that the child action is "telling" the parent action to refresh itself by using the parent's ViewData.
It's kind of a hack, but it works fine for what's you're doing.