Ehsan Akbar
Ehsan Akbar

Reputation: 7299

Pass a value to an action in controller

I have an action that redirect to another action:

public ActionResult Create(Score score)
{
     score.DateOfSubmit = DateTime.Now.Date;
     obj.AddNewScore(score);
     obj.Save();
     return RedirectToAction("Index", "Score");
}

I want to pass an id to index action in last line .so how can pass an id to index action .

Upvotes: 3

Views: 94

Answers (2)

Ehsan Sajjad
Ehsan Sajjad

Reputation: 62498

Do like this:

public ActionResult Create(Score score)
{
    score.DateOfSubmit = DateTime.Now.Date;
    obj.AddNewScore(score);
    obj.Save();
    return RedirectToAction("Index", "Score", new {id=score.Id});
}

and in Index action do like this:

public ActionResult Index(int id)
{
    return View();
}

Upvotes: 2

Ivan Doroshenko
Ivan Doroshenko

Reputation: 942

return RedirectToAction("Index", "Score", new { id = score.Id });

Upvotes: 2

Related Questions