user1322549
user1322549

Reputation: 11

RouteData lost on Post in a controller called by RenderAction

I have a "Resume" view which makes a call to RenderAction so I can add comments to a resume. Code:

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

So the code above calls Add action in my ResumeComment controller and passes it resumeId. If I take a look at my ControllerContext.ParentActionViewContext.RouteData inside the Add (GET) method, I see 4 values:

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?

Upvotes: 0

Views: 515

Answers (2)

Petur Subev
Petur Subev

Reputation: 20193

You can use the TempData- its like session but once you read the data from it is deleted. Its a good solution to store temporary data between two sequential requests.

Upvotes: 0

Jakub Konecki
Jakub Konecki

Reputation: 46008

If you want to post some values in your form to ResumeComment.Add you need to put them in hidden inputs. RouteData is not in any magic way persisted between requests.

Upvotes: 0

Related Questions