【问题标题】:RouteData lost on Post in a controller called by RenderActionRouteData 在由 RenderAction 调用的控制器中的 Post 上丢失
【发布时间】:2012-04-09 19:38:06
【问题描述】:

我有一个“简历”视图,它调用 RenderAction,以便我可以将 cmets 添加到简历中。代码:

    @{Html.RenderAction("Add", "ResumeComment", new { resumeId = @Model.Id });}

所以上面的代码在我的 ResumeComment 控制器中调用 Add action 并将它传递给 resumeId。如果我查看 Add (GET) 方法中的 ControllerContext.ParentActionViewContext.RouteData,我会看到 4 个值:

PL <- which is resume category
954 <- resume id
Resume <- Controller
Edit <- Action, because I am adding comments on the Edit page in the resume.

The problem that I have is that I am loosing resume category (PL) and resume id(954) when I post (add a comment). Here is my ResumeComment form:

    @using (Html.BeginForm("Add", "ResumeComment"))
    {
    ...
    <input type="submit" value="Add Comment" />   

    ..}

So this form will call Add (Post) method in the ResumeComment controller when sumitted. Here is my add method:


    [HttpPost, ActionName("Add")]  
    public ActionResult Add(ResumeComment resumeComment)
    {
    .....
    }

I am not able to access ControllerContext.ParentActionViewContext.RouteData at all, it is null. I am however able to access ControllerContext.RouteData but when I look at the values I only see "ResumeComment" and "Add" in there and that is it. How can I preserve the resume category and resume id?

【问题讨论】:

    标签: asp.net-mvc renderaction routedata


    【解决方案1】:

    如果您想将表单中的某些值发布到ResumeComment.Add,您需要将它们放入隐藏输入中。 RouteData 在请求之间不会以任何神奇的方式持续存在。

    【讨论】:

      【解决方案2】:

      您可以使用TempData- 类似的会话,但是一旦您从中读取数据就会被删除。 它是在两个顺序请求之间存储临时数据的好解决方案。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-11-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-04-05
        • 1970-01-01
        相关资源
        最近更新 更多