I have a POST
method in my controller for creating a new entry in the database. Once the operation is done, it should redirect me to the PhaseController -> Index -> value of the Id that I got using TempData
. See the Create() method below:
PhaseController.cs
[HttpPost]
public IActionResult Create(Phase phase)
{
int releaseId = (int)TempData["id"];
string connectionString = Configuration["ConnectionStrings:DefaultConnection"];
using (SqlConnection connection = new SqlConnection(connectionString))
{
string sql = "CreatePhase";
using (SqlCommand command = new SqlCommand(sql, connection))
{
command.CommandType = CommandType.StoredProcedure;
// adding parameters
SqlParameter parameter = new SqlParameter
{
ParameterName = "@Name",
Value = phase.Name,
SqlDbType = SqlDbType.VarChar,
Size = 50
};
command.Parameters.Add(parameter);
parameter = new SqlParameter
{
ParameterName = "@ReleaseId",
Value = releaseId,
SqlDbType = SqlDbType.Int
};
command.Parameters.Add(parameter);
connection.Open();
command.ExecuteNonQuery();
connection.Close();
}
}
return RedirectToAction("Index", "Phase", releaseId);
}
As you can see, I use return RedirectToAction("Index", "Phase", releaseId);
which I guess it is supposed to redirect me to localhost/Phase/Index/1
. Instead, I get redirected only to /Phase
(which is the same as Phase/Index I suppose). Below I will also include the
Create.cshtml
@model Phase
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>ADO - Create Phase</title>
<link rel="stylesheet" asp-href-include="lib/bootstrap/dist/css/*.min.css" />
</head>
<body>
<div class="container-fluid">
<h1>Create a Phase</h1>
<form method="post">
<div hidden class="form-group">
<label asp-for="Id"></label>
<input class="form-control" asp-for="Id" />
</div>
<div class="form-group">
<label asp-for="Name"></label>
<input class="form-control" asp-for="Name" />
</div>
<div hidden class="form-group">
<label asp-for="ReleaseId"></label>
<input class="form-control" asp-for="ReleaseId" />
</div>
<div class="text-center panel-body">
<button type="submit" class="btn btn-sm btn-primary">Create</button>
</div>
</form>
</div>
</body>
</html>
EDIT: Index method
public ActionResult Index(int id)
{
TempData["id"] = id;
List<Phase> phaseList = new List<Phase>();
string connectionString = Configuration["ConnectionStrings:DefaultConnection"];
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
string sql = "ReadPhases";
SqlCommand command = new SqlCommand(sql, connection);
command.CommandType = CommandType.StoredProcedure;
SqlParameter parameter = new SqlParameter
{
ParameterName = "@ReleaseId",
Value = id,
SqlDbType = SqlDbType.VarChar,
Size = 50
};
command.Parameters.Add(parameter);
using (SqlDataReader dataReader = command.ExecuteReader())
{
while (dataReader.Read())
{
Phase phase = new Phase();
phase.Id = Convert.ToInt32(dataReader["Id"]);
phase.Name = Convert.ToString(dataReader["Name"]);
phase.ReleaseId = Convert.ToInt32(dataReader["ReleaseId"]);
phaseList.Add(phase);
}
}
connection.Close();
}
return View(phaseList);
}
What is going wrong here? What could be the causes for which I am not being redirected properly?