Search code examples
c#asp.netasp.net-mvcasp.net-mvc-5

ASP POST ActionResult recieves a 0 instead of a provided ID value


Well, hello everyone.

I have a short ASP .NET MVC5 question. I have to pass a value from a ViewBag in the View method with a POST action result.

Create Method View Trigger

@Html.ActionLink("Add TestCase", "Create", "TestPlanTestCases", new { id = Model.TestPlan.ID }, null)

Controller Create GET method

    public ActionResult Create(int id)
    {
        var testPlanFind = _db.TestPlan.Find(id);
        ViewBag.TestPlanID = testPlanFind;
        ViewBag.TestCaseID = new SelectList(_db.TestCase,"ID", "Name")
        return View();
    }

Create View, related DIV:

        <div class="form-group">
        @Html.LabelFor(model => model.TestPlanID, "TestPlanID", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.ViewBag.TestPlanID.Name
            @Html.ValidationMessageFor(model => model.TestPlanID, "", new { @class = "text-danger" })
        </div>

Controller Create POST method

        public ActionResult Create([Bind(Include = "TestPlanID,TestCaseID")] TestPlanTestCases testPlanTestCases)
    {
        if (ModelState.IsValid)
        {
            _db.TestPlanTestCases.Add(testPlanTestCases);
            _db.SaveChanges();
            return RedirectToAction("Details", "TestPlans", new {id = testPlanTestCases.TestPlanID});
        }
        ViewBag.TestCaseID = new SelectList(_db.TestCase, "ID", "Name", testPlanTestCases.TestCaseID);
        ViewBag.TestPlanID = new SelectList(_db.TestPlan, "ID", "Name", testPlanTestCases.TestPlanID);
        return View(testPlanTestCases);

So, my problem is, when the POST method is being called, the method always receives TestPlanID = 0 and TestCaseID = (ID of the chosen test case). I've used the same workaround for a different controller that has similar functionality and it works perfectly fine, but for some reason, when it comes to setting a predefined value such as TestPlanID it is automatically being set to 0, not even a null. The GET method works fine and passes the right ID, but when it comes down to the POST method, something goes wrong.

Result of Create GET method in View Debug: Values of TestPlanID and TestCaseID

I hope I have provided enough information for you to understand the issue. Thanks in advance!


Solution

  • Well. I've finally solved it, maybe not the best solution, but it works.

    A dev friend of mine suggested parsing the URL to get the ID in the POST method. So I've decided to add 3 additional lines of code to the controller, first one to parse the URL for ID value, second to convert the string to Int32, and then assign the result value to the testPlanTestCases.TestPlan.

    Updated Controller:

    public ActionResult Create([Bind(Include = "TestCaseID")] TestPlanTestCases testPlanTestCases)
    {
        var testPlanId = RouteData.Values["id"];
        testPlanId = Convert.ToInt32(testPlanId);
        testPlanTestCases.TestPlanID = (int) testPlanId;
        if (ModelState.IsValid)
        {
            _db.TestPlanTestCases.Add(testPlanTestCases);
            _db.SaveChanges();
            return RedirectToAction("Details", "TestPlans", new {id = testPlanTestCases.TestPlanID});
        }
        ViewBag.TestCaseID = new SelectList(_db.TestCase, "ID", "Name", testPlanTestCases.TestCaseID);
        ViewBag.TestPlanID = new SelectList(_db.TestPlan, "ID", "Name", testPlanTestCases.TestPlanID);
        return View(testPlanTestCases);