locked
Getting error: An unhandled exception occurred while processing the request. RuntimeBinderException: Cannot perform runtime binding on a null reference CallSite.Target(Closure , CallSite , object ) RRS feed

  • Question

  • User-773717037 posted

    The error is happening on 11.

    • CallSite.Target(Closure , CallSite , object )

    • System.Dynamic.UpdateDelegates.UpdateAndExecute1<T0, TRet>(CallSite site, T0 arg0)

    • AspNetCore._Views_Student_EnrollCourse_cshtml+<ExecuteAsync>d__17.MoveNext() in EnrollCourse.cshtml

      <button class="expandCollapseButton" data-frameid="frame3">-</button>
      1. <tr>
      2. <th>Course ID</th>
      3. <th>Course Title</th>
      4. <th>Remaining Seats</th>
      5. <tr>
      6. <tr>
      1. <td>@ViewBag.course.CourseId</td>
      1. <td>@ViewBag.course.CourseTitle</td>
      2. <td>@ViewBag.course.SeatCapacity</td>
      3. </tr>
      4. </table>
      5. @{
      6. if (ViewBag.Course.SeatCapacity < 1)

      Here is my code:

      @model Chapter6Example1.Models.Enrollment
      <P>Enter Student ID to register this course</P>
      <h3>Course Information</h3>
      <table border="1">
      <tr>
      <th>Course ID</th>
      <th>Course Title</th>
      <th>Remaining Seats</th>
      <tr>
      <tr>
      <td>@ViewBag.course.CourseId</td>
      <td>@ViewBag.course.CourseTitle</td>
      <td>@ViewBag.course.SeatCapacity</td>
      </tr>
      </table>
      @{
      if (ViewBag.Course.SeatCapacity < 1)
      {
      <p>This course is full. Try at a different time.</p>
      <p>
      <a asp-controller="Course" asp-action="AllCourse">
      Back to course list
      </a>
      </p>
      }
      else
      {
      <form asp-controller="Student"
      asp-action="EnrollCourse" method="post">
      <label asp-for="StudentId"></label>
      <input asp-for="StudentId" />
      <br />
      <input asp-for="CourseId" type="hidden"
      value=@ViewBag.course.CourseId />
      <button type="submit">Register Course</button>
      </form>
      }
      }

      It's just like the textbook says to put it in.

      Any help would be greatly appreciated.

    Wednesday, June 5, 2019 9:45 PM

All replies

  • User-1764593085 posted

    Hi Darrin.Rodden,

    The problem is that you do not set a ViewBag.course or ViewBag.course is null in controller.You need to check the action where you pass data to view using ViewBag.

    Refer to pass data to view

    Best Regards,

    Xing

    Thursday, June 6, 2019 3:20 AM
  • User-773717037 posted

    Xing,

    First Thank you so much...But I may have been wrong in my post. After correcting some oversights in coding my issue now is this:

    An unhandled exception occurred while processing the request.

    AmbiguousActionException: Multiple actions matched. The following actions matched route data and had all constraints satisfied:

    Chapter6Example1.Controllers.StudentController.EnrollCourse (Chapter6Example1)
    Chapter6Example1.Controllers.StudentController.EnrollCourse (Chapter6Example1)

    Microsoft.AspNetCore.Mvc.Internal.ActionSelector.SelectBestCandidate(RouteContext context, IReadOnlyList<ActionDescriptor> candidates)

    Here is my code for StudentController.cs:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    using Chapter6Example1.Models;
    using Microsoft.AspNetCore.Mvc;
    using Microsoft.EntityFrameworkCore;

    // For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

    namespace Chapter6Example1.Controllers
    {
    public class StudentController : Controller
    {
    SchoolDbContext db;
    public StudentController(SchoolDbContext db)
    {
    this.db = db;
    }
    public async Task<IActionResult> AllStudent()
    {
    var student = await db.Students.ToListAsync();
    return View(student);
    }
    public IActionResult AddStudent()
    {
    return View();
    }
    [HttpPost]
    public async Task<IActionResult> AddStudent(Student student)
    {
    db.Add(student);
    await db.SaveChangesAsync();
    return RedirectToAction("AllStudent");
    }
    public async Task<IActionResult> EnrollCourse(int? id)
    {
    var course = await db.Courses.
    SingleOrDefaultAsync(c => c.CourseId == id);
    ViewBag.Course = course;
    return View();
    }

    public async Task<IActionResult>EnrollCourse(Enrollment enrollment)
    {
    db.Add(enrollment);
    var course = await db.Courses.FindAsync(enrollment.CourseId);
    course.SeatCapacity--;
    await db.SaveChangesAsync();
    return RedirectToAction("AllCourse", "Course");
    }

    public async Task<IActionResult> AllClassmate(int? id)
    {
    var enrollInCourse = await db.Enrollments.Where
    (e => e.CourseId == id).ToListAsync();
    List<Student> classmate = new List<Student>();
    foreach (var e in enrollInCourse)
    {
    var student = await db.Students.
    SingleOrDefaultAsync
    (s => s.StudentId == e.StudentId);
    classmate.Add(student);
    }
    ViewData["course"] = db.Courses.Find(id).CourseTitle;
    return View(classmate);
    }
    }
    }

    Please note: after [HttpPost] all the method's with the blank line between the code I believe is to have something like [HttpPut] or [HtppGet] or something...this is the part I do not understand.

    What I need to know is which is the correct one to use...I just do not at this time understand what is needed to fix the above error...

    If you can help Xing I would appreciate it very much

    Thanks In Advance,

    Darrin

    Thursday, June 6, 2019 6:24 PM
  • User475983607 posted

    There are two EnrollCourse GET actions. 

    [HttpGet]
    public async Task<IActionResult> EnrollCourse(int? id)
    {
    var course = await db.Courses.
    SingleOrDefaultAsync(c => c.CourseId == id);
    ViewBag.Course = course;
    return View();
    }
    
    [HttpPost]
    public async Task<IActionResult>EnrollCourse(Enrollment enrollment)
    {
    db.Add(enrollment);
    var course = await db.Courses.FindAsync(enrollment.CourseId);
    course.SeatCapacity--;
    await db.SaveChangesAsync();
    return RedirectToAction("AllCourse", "Course");
    }

    Thursday, June 6, 2019 7:06 PM
  • User-773717037 posted

    Hello mgebhard,

    According to your Post I would need to add [HttpGet] to that method and [HttpPost] to the other method...I tried that and it gave me another error:

    An unhandled exception occurred while processing the request.

    RuntimeBinderException: Cannot perform runtime binding on a null reference

    CallSite.Target(Closure , CallSite , object )

    • Stack 
    • Query 
    • Cookies 
    • Headers
    • RuntimeBinderException: Cannot perform runtime binding on a null reference

      • CallSite.Target(Closure , CallSite , object )

      • System.Dynamic.UpdateDelegates.UpdateAndExecute1<T0, TRet>(CallSite site, T0 arg0)

      • AspNetCore._Views_Student_EnrollCourse_cshtml+<ExecuteAsync>d__17.MoveNext() in EnrollCourse.cshtml

        <button class="expandCollapseButton" data-frameid="frame3">+</button>
        1. <td>@ViewBag.course.CourseId</td>

        According to my code for EnrollCourse.cshtml:

        @model Chapter6Example1.Models.Enrollment
        <P>Enter Student ID to register this course</P>
        <h3>Course Information</h3>
        <table border="1">
        <tr>
        <th>Course ID</th>
        <th>Course Title</th>
        <th>Remaining Seats</th>
        <tr>
        <tr>
        <td>@ViewBag.course.CourseId</td>
        <td>@ViewBag.course.CourseTitle</td>
        <td>@ViewBag.course.SeatCapacity</td>
        </tr>
        </table>
        @{
        if (ViewBag.Course.SeatCapacity < 1)
        {
        <p>This course is full. Try at a different time.</p>
        <p>
        <a asp-controller="Course" asp-action="AllCourse">
        Back to course list
        </a>
        </p>
        }
        else
        {
        <form asp-controller="Student"
        asp-action="EnrollCourse" method="post">
        <label asp-for="StudentId"></label>
        <input asp-for="StudentId" />
        <br />
        <input asp-for="CourseId" type="hidden"
        value=@ViewBag.course.CourseId />
        <button type="submit">Register Course</button>
        </form>
        }
        }

        This file is from our textbook and is code on.

        I don't think that is the fix.

    Thursday, June 6, 2019 8:27 PM
  • User475983607 posted

    This error was explained above.  But more importantly the ViewBag is not correct.  You must cast the ViewBag to a type first.  Please see the following reference documentation which contains code examples.

    https://docs.microsoft.com/en-us/aspnet/core/mvc/views/overview?view=aspnetcore-2.2#weakly-typed-data-viewdata-viewdata-attribute-and-viewbag

    Also, try running your code through the Visual Studio debugger to verify you code is working as expected.

    https://docs.microsoft.com/en-us/visualstudio/debugger/debugger-feature-tour?view=vs-2019

    Thursday, June 6, 2019 8:54 PM
  • User-1764593085 posted

    Hi Darrin.Rodden,

     

    public async Task<IActionResult> EnrollCourse(int? id)
    {
    var course = await db.Courses.
    SingleOrDefaultAsync(c => c.CourseId == id);
    ViewBag.Course = course;
    return View();

    }

    So do you check whether `course` is null or not in your Get method(above code)?You could add a breakpoint there to debug it.

    Friday, June 7, 2019 1:38 AM