ASP.NET MVC 应用程序失败,出现 null 异常错误

ASP.NET MVC application failing with a null exception error

提问人:George Turner 提问时间:2/14/2021 最后编辑:George Turner 更新时间:2/14/2021 访问量:107

问:

当前在单击以下 Web 方法时失败:AddApplication

参数字典包含“NAA”中方法“System.Web.Mvc.ActionResult AddApplication(System.String, Int32, System.String)”的不可为 null 类型“System.Int32”的参数“UniversityId”的 null 条目。Controllers.ApplicationController”。

我将提供我的 DAO 代码、服务和控制器中的代码

道:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NAA.Data;
using NAA.Data.IDAO;
using NAA.Data.DAO;
using NAA.Data.Repository;
using NAA.Data.Models.Domain;

namespace NAA.Data.DAO
{
    public class ApplicationDAO : IApplicationDAO
    {
        //ADDAPPLICATION METHOD NEEDED HERE(Refer to Addmusic tutorial for tips)
        public void AddUniversityToCollection(Application application, University university, NAAContext context)
        {
            context.Universities.Find(university.UniversityId).Applications.Add(application);
            context.SaveChanges();
        }

        public void AddApplication(Application application, NAAContext context)
        {
            context.Applications.Add(application); //this adds applications what's stored in application and pulls to interface
            //context.SaveChanges(); // this saves changes to the database
        }
    }
}

服务内容:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NAA.Data.Repository;
using NAA.Data.DAO;
using NAA.Data.IDAO;
using NAA.Data.Models;
using NAA.Data.Models.Domain;
using NAA.services.IService;

namespace NAA.services.Service
{
    public class ApplicationService : IApplicationService
    {
        private IApplicationDAO applicationDAO;
        private IUniversityDAO universityDAO;
        private IUserDAO userDAO;

        public ApplicationService()
        {
            applicationDAO = new ApplicationDAO();
            universityDAO = new UniversityDAO();
            userDAO = new UserDAO();
        }

        public void AddApplication(NAA.services.Models.UniversityApplication application, string UserId)
        {
            Application newApplication = new Application()
            {
                Course = application.Course,
                Statement = application.Statement,
                TeacherContact = application.TeacherContact
            };

            using (var context = new NAAContext())
            {
                applicationDAO.AddApplication(newApplication, context);
                User user = userDAO.GetUser(application.UserId, context);
                userDAO.AddUniversityToCollection(newApplication, user, context);
                University university = universityDAO.GetUniversity(application.UniversityId, context);
                universityDAO.AddUniversityToCollection(newApplication, university, context);
                context.SaveChanges();
            }
        }
    }
}

和控制器:

using NAA.Data.Models.Domain;
using NAA.OutService;
using NAA.services.Models;
using NAA.services.Service;
using NAA.Data.DAO;
using NAA.Data.Repository;
using NAA.Data.IDAO;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using NAA.services.IService;

namespace NAA.Controllers
{
    public class ApplicationController : Controller
    {
        IApplicationService applicationService;

        public ApplicationController()
        {
            applicationService = new ApplicationService();
        }

        // GET: Application
        [HttpGet]
        public ActionResult AddApplication(string UserId, int UniversityId, string Course)
        {
            ViewBag.UserId = UserId;
            ViewBag.UniversityId = UniversityId;
            ViewBag.Name = Course;
            return View();
        }

        [HttpPost]
        public ActionResult AddApplication(UniversityApplication application, string UserID)
        {
            applicationService.AddApplication(application, UserID);
            return RedirectToAction("GetUniversities", "User", new { id = application.UserId });
        }

        //public ActionResult GetUniversities(int profileId)
        //{
        //    ViewBag.profileId = profileId;
        //}
    }
}

有没有人能解释我哪里出了问题?

编辑: 此处包含调用 AddApplication Web 方法的代码:

@model IEnumerable<NAAInServices.ProxyToSHUWebService.SHUCourse>

@{
    ViewBag.Title = "GetSHUCourses";
}

<h2>GetSHUCourses</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.CourseId)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.CName)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.CRequirements)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.CDescription)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.CDegree)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.CTarif)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.CNSS)
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.CourseId)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.CName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.CRequirements)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.CDescription)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.CDegree)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.CTarif)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.CNSS)
        </td>
        <td>
            @Html.ActionLink("Apply", "AddApplication", new { UserId = ViewBag.UserID, UniversityId = ViewBag.UniversityId, Course = ViewBag.Course, Controller = "Application" }) |
            @Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
            @Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
        </td>
    </tr>
}

</table>

C# ASP.NET-MVC NullReferenceException

评论

1赞 Guillermo Gerard 2/14/2021
能否分享对 AddApplication 控制器操作的调用?可能您缺少 UniversityId。您可以尝试将控制器中的 UniversityId 从 int 更改为 int?以查看是否可以获取有关是否正确设置了 UniversityId 的更多信息
0赞 Serhii 2/14/2021
尝试将断点放在 GET AddApplication 中并检查输入参数。同样,仅用于测试,您可以将类型从 int 更改为 string 并检查参数。如果您与我们分享您的通话网址,那就太好了。
0赞 George Turner 2/15/2021
嗨,我已经使用包含对 AddApplication 操作的调用的代码编辑了原始帖子。多谢!

答: 暂无答案