ASP.NET MVC 应用程序因空异常错误而失败

时间:2021-02-14 15:06:53

标签: c# asp.net-mvc nullreferenceexception

当前在点击 AddApplication 网络方法时失败,如下所示:

<块引用>

参数字典包含一个非空类型“System.Int32”的参数“UniversityId”的空条目,用于方法“System.Web.Mvc.ActionResult AddApplication(System.String, Int32, System.String)” in ' NAA.Controllers.ApplicationController'.

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

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>

0 个答案:

没有答案