0

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?

Questieme
  • 913
  • 2
  • 15
  • 34
  • What does the Index method of the phase controller look like? Is it configured to take an argument? – jasonmchoe Dec 17 '19 at 13:38
  • 2
    You may find your answer [here](https://stackoverflow.com/questions/1257482/redirecttoaction-with-parameter). – ylmz Dec 17 '19 at 13:38
  • How's the route configuration for `Phase/Index` URL look like? – Chetan Dec 17 '19 at 13:38
  • Indeed @jasonmchoe. `public ActionResult Index(int id)`. I edited the Original Post including the Index method – Questieme Dec 17 '19 at 13:40
  • @ChetanRanpariya I am not sure whether I set one? The thing is - Index takes an argument, therefore whenever I will go through that method, I will have an URL like `https://localhost:../Phase/Index/1` or other ID. – Questieme Dec 17 '19 at 13:42
  • @ylmz I actually looked through that one. I did not go through all solutions there yet, but the first answer, `return RedirectToAction("Index", "Phase", new { releaseId });` , returns me to a URL which looks like: `https://localhost:44397/Phase?releaseId=1` instead of `Phase/Index/?releaseId=1` – Questieme Dec 17 '19 at 13:44

1 Answers1

2

Plus my comment, I think you should use it like:

return RedirectToAction("Index", "Phase", new { id = releaseId });
ylmz
  • 46
  • 3