将值传递给控制器中的操作

本文关键字:操作 控制器 值传 | 更新日期: 2023-09-27 17:55:17

>我有一个重定向到另一个操作的操作:

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

我想在最后一行将 id 传递给索引操作,那么如何将 id 传递给索引操作。

将值传递给控制器中的操作

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

这样做:

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

Index行动中,这样做是这样的:

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