Asp.Net Mvc 2 - 带有创建的 RenderAction 列表

发布于 2024-08-23 11:06:23 字数 834 浏览 8 评论 0原文

首先,我使用 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 技术交流群。

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

发布评论

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

评论(1

谷夏 2024-08-30 11:06:23

您可以通过涉及 ViewContext.ParentActionViewContext 的小技巧强制父视图刷新自身。

在您的 CommentController 类中:

public ActionResult Create(Comment comment)
{
    ...
    if (isValid) // Comment entered in form is valid
    {
        ControllerContext.ParentActionViewContext.ViewData["SuccessfullCreate"] = true;
    }
    ...
}

在您的 Comment/List.aspx 页面(视图)中:

<% Html.RenderAction("Create", "Comment"); %>
<%
    if (ViewContext.ViewData["SuccessfulCreate"] != null)
    {
        string action = ViewContext.RouteData.Values["action"].ToString();
        string controller = ViewContext.RouteData.Values["controller"].ToString();
        string url = "/" + controller + "/" + action;

        Response.Redirect(url);
    }
%>

所以基本上,发生的情况是子操作“告诉”父操作通过使用父操作的 ViewData 来刷新自身。

这有点像黑客,但对于你正在做的事情来说它工作得很好。

You can force the parent View to refresh itself with a small hack involving ViewContext.ParentActionViewContext.

In your CommentController class:

public ActionResult Create(Comment comment)
{
    ...
    if (isValid) // Comment entered in form is valid
    {
        ControllerContext.ParentActionViewContext.ViewData["SuccessfullCreate"] = true;
    }
    ...
}

And in your Comment/List.aspx page (view) :

<% Html.RenderAction("Create", "Comment"); %>
<%
    if (ViewContext.ViewData["SuccessfulCreate"] != null)
    {
        string action = ViewContext.RouteData.Values["action"].ToString();
        string controller = ViewContext.RouteData.Values["controller"].ToString();
        string url = "/" + controller + "/" + action;

        Response.Redirect(url);
    }
%>

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.

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