AnonyMouse
AnonyMouse

Reputation: 18630

MVC get URL index value after jquery call

Hi have an MVC page that shows up with the url like:

http://myhost/Invoice/Edit/2

When I'm at this page I run some jquery that calls another method in the controller. In this new method I want to get the 2 from http://myhost/Invoice/Edit/2. One problem is now that I've called the new method the Request.Url has obviously changed from http://myhost/Invoice/Edit/2 to be different.

Can anyone tell me in this situation how I would get the 2?

Upvotes: 0

Views: 531

Answers (2)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038780

I run some jquery that calls another method in the controller.

You haven't shown how exactly this script is calling your other controller action but you should pass the initial id as parameter to this action. For example if you were doing an AJAX call or something:

$.post('@Url.Action("Edit")', new { invoiceId = '@Model.Id' }, function() {
    ...    
});

and in the controller action:

[HttpPost]
public ActionResult Edit(int invoiceId)
{
    ...
}

Upvotes: 1

Van Thoai Nguyen
Van Thoai Nguyen

Reputation: 1036

ControllerContext.ParentActionViewContext.RouteData.Values["id"] 

Upvotes: 0

Related Questions