1

I'm beginner in MVC3, and I want to get a value from an another controller's method. Here the two methods:

    [HttpPost]
    public ActionResult Create(TennisClub tennisclub)
    {
        if (ModelState.IsValid)
        {
            db.TennisClubs.Add(tennisclub);
            db.SaveChanges();
            return RedirectToAction("AssignManager");  
        }

        return View(tennisclub);
    }

    [HttpPost]
    public ActionResult AssignManager(Manager manager)
    {

    }

So, when I'm creating a new tennis club, Immediately I would like to assign a manager to it... For that I need the primary key "ID".

So my question is: How to get this ID in my "AssignManager" method ? Thanks in advance

3
  • Why dont you assign the manager right there when you add the Tennis club? Commented Mar 26, 2012 at 7:43
  • Because I need to create it before assign it to the tennis club, Sorry I forget this detail... Commented Mar 26, 2012 at 7:44
  • Francesco, just assign by reference, the the data context will take car of this for you. Commented Mar 26, 2012 at 7:49

3 Answers 3

3

You cannot redirect to an action decorated with the [HttpPost] attribute. That's not how a redirect works. A redirect means that you are sending a 301 HTTP status code to the client with the new Location header and the client issues a GET request to this new location.

So once you remove the [HttpPost] attribute from your AssignManager action you could pass the id as parameter:

return RedirectToAction("AssignManager", new { id = "123" });  

and then:

[HttpPost]
public ActionResult AssignManager(int id)
{

}
Sign up to request clarification or add additional context in comments.

Comments

1

Basically, you need to have a GET AssignManager method, too, which would have a parameter telling it to which TennisClub the manager should be assigned:

[HttpGet]
public ActionResult AssignManager(int tennisClubId)
{
    // here, you will want to return AssignManager view
}

And when redirecting to AssignManager from Create, you can specify the id of TennisClub:

return RedirectToAction("AssignManager", new { tennisClubId = tennisclub.Id });

1 Comment

Thank you for your response + 1
1
return RedirectToAction("AssignManager", new { id = tennisclub.Id }); 

Also you need to remove the [HttpPost] attribute from your action

public ActionResult AssignManager(int id) {
  //...
}

1 Comment

Thank you for your response + 1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.