2 Replies - 680 Views - Last Post: 07 September 2016 - 05:52 PM

#1 andrewsw  Icon User is offline

  • the case is sol-ved
  • member icon

Reputation: 6379
  • View blog
  • Posts: 25,770
  • Joined: 12-December 12

id parameter not being passed

Posted 07 September 2016 - 01:38 PM

I've been quite pleased with this tutorial so far but I have an issue at creating the Edit View page.

It might be caused because I right-clicked and Add View but then had to delete this page and start over. With Add View would it automatically add routing for it? And not do so if I delete and recreate the View?

Anyway, on arriving at http://localhost:59160/Student/Edit/2 it provides the error

Quote

The parameters dictionary contains a null entry for parameter 'studentId' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult Edit(Int32)' in 'MVC_Basics.Controllers.StudentController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.
Parameter name: parameters

The id 2 is within the url so for some reason isn't getting through.

I added this routing information:
            routes.MapRoute(
                name: "StudentEdit",
                url: "student/{edit}/{id}",
                defaults: new { controller = "Student", action = "Edit" }
            );

I also tried adding id = UrlParameter.Optional but this doesn't seem right, it shouldn't be optional.

Here are the controller methods that I have:

        [HttpGet]
        public ActionResult Edit(int studentId) {
            // Get the student from the student list sample collection.
            // You can get the student from the database in real applications.
            var std = studentList.Where(s => s.StudentId == studentId).FirstOrDefault();

            return View(std);
        }

        [HttpPost]
        public ActionResult Edit(Student std) {
            // write code to update the student

            return RedirectToAction("Index");
        }

I tried adding the HttpGet to the first method, but it made no difference.

I must be missing something simple, can you spot it? I can show some more code if needed.

(I shouldn't really have to correct a beginners tutorial, but I suppose it will prove useful that I've tried to fix it myself.)

Is This A Good Question/Topic? 0
  • +

Replies To: id parameter not being passed

#2 andrewsw  Icon User is offline

  • the case is sol-ved
  • member icon

Reputation: 6379
  • View blog
  • Posts: 25,770
  • Joined: 12-December 12

Re: id parameter not being passed

Posted 07 September 2016 - 01:49 PM

Darn, I've been working on this for an hour or so (on and off) and within a minute of posting I had an inspiration:

            routes.MapRoute(
                name: "StudentEdit",
                url: "student/{edit}/{id}",
                defaults: new { controller = "Student", action = "Edit" }
            );

{id} should be {studentid}.

Still, maybe I'll have a further question to add ;)
Was This Post Helpful? 0
  • +
  • -

#3 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 963
  • View blog
  • Posts: 2,382
  • Joined: 15-February 11

Re: id parameter not being passed

Posted 07 September 2016 - 05:52 PM

Hello,
Unless you intend for {edit} to be a parameter in your route you should remove the curly braces.
Currently {edit} has the value of Edit when your Edit(int) action is called.

Fix
url: "student/edit/{studentId}",

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1