ArgumentNullException:值不能为null。参数名称:实体

时间:2019-07-17 13:23:40

标签: c# entity-framework razor-pages argumentnullexception

每当我尝试保存在网站上输入的数据时,都会遇到以下内部服务器错误: ArgumentNullException:值不能为null。参数名称:实体

我能够从数据库中写入并在网站上看到它,但是我无法在网站上进行写入以将其存储在数据库中并在网站上显示。我正在使用Razor Pages

C#代码显示在此处。 PFA。

C#代码:

public class AddPreferenceModel : PageModel
    {
        [BindProperty]
        public CUser User{ get; set; }
        public CStudent Stu{ get; set; }
        public CPreference Prefer { get; set; }

        private readonly IHostingEnvironment _environment;
        private readonly CWebApplication _context;
        private readonly IEmailService _emailServices;
        private readonly IUserService _UserService;

        public AddPreferenceModel(IUserService UserService, CWebApplication context, IHostingEnvironment IHostingEnvironment, INTUserService INTUserService, IEmailService emailService)
        {
            _environment = IHostingEnvironment;
            _UserService = UserService;
            _context = context;
            _emailServices = emailService;

            User = _UserService.GetUser();
        }

        public ActionResult OnGet()             
        {
            if (NTUser.Role != Role.Admin)
            {
                return RedirectToPage("/Denied");
            }
            return Page();
        }

        [HttpPost]
        public async Task<IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return Page();
            }

            if (NTUser.Role != Role.Admin && false)
            {
                return RedirectToPage("/Denied");
            }
            _context.Preference.Add(Prefer);
            await _context.SaveChangesAsync();

            return RedirectToPage("Index");
        }
    }

HTML:

<div class="Custom-Content Custom-Max-Width-800">
    <h2 class="h2">Add your preference</h2>

    <p>
        Please check all the details before submitting
    </p>
    <form enctype="multipart/form-data" id="orderForm" method="post">
        <div class="col-md-12"><hr/></div>
        <h4 class="h4 Custom-H4-Less-Margin-Bottom">Preference</h4>
        <div class="Custom-Form col-sm-6">
            NTUser
            <input asp-for="Prefer.StudentUserID" type="text" value="" required="" />
        </div>
        <div class="Custom-Form col-sm-6">
            Preference One
            <input asp-for="Prefer.name" type="text" value="" required="" />
        </div>
        <div class="Custom-Form col-sm-12">
            Priority

            <select asp-for="Prefer.Priority" required>
                <option value="High">High</option>
                <option value="Medium">Medium</option>
                <option value="Low">Low</option>
            </select>
        </div>
        <hr/>
        <button class="Custom-Button" type="submit" name="save">Save</button>
    </form>
</div>

首选项类别:

 public enum Priority { HIGH = 0, MEDIUM = 1, LOW = 2 }

        public class CPreference
        {
            public string StudentUserID{ get; set; }
            public Priority Priority { get; set; }
            public string name { get; set; }

            public CStudent Student { get; set; }
         }

在网站上输入的值需要存储在数据库的表中并显示在“索引”页面中。但是每当我点击保存时,

的内部服务器错误
  

ArgumentNullException:值不能为null。参数名称:实体

被抛出。如前所述,我能够从数据库写入页面。

完整堆栈:

Microsoft.EntityFrameworkCore.Utilities.Check.NotNull<T>(T value, string parameterName)
Microsoft.EntityFrameworkCore.DbContext.Add<TEntity>(TEntity entity)
Microsoft.EntityFrameworkCore.Internal.InternalDbSet<TEntity>.Add(TEntity entity)
LearnAngebot.Pages.Preference.AddPreferenceModel.OnPostAsync() in AddPreference.cshtml.cs

                _context.Preference.Add(Prefer);

Microsoft.AspNetCore.Mvc.RazorPages.Internal.ExecutorFactory+GenericTaskHandlerMethod.Convert<T>(object taskAsObject)
Microsoft.AspNetCore.Mvc.RazorPages.Internal.ExecutorFactory+GenericTaskHandlerMethod.Execute(object receiver, object[] arguments)
Microsoft.AspNetCore.Mvc.RazorPages.Internal.PageActionInvoker.InvokeHandlerMethodAsync()
Microsoft.AspNetCore.Mvc.RazorPages.Internal.PageActionInvoker.InvokeNextPageFilterAsync()
Microsoft.AspNetCore.Mvc.RazorPages.Internal.PageActionInvoker.Rethrow(PageHandlerExecutedContext context)
Microsoft.AspNetCore.Mvc.RazorPages.Internal.PageActionInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.RazorPages.Internal.PageActionInvoker.InvokeInnerFilterAsync()
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextResourceFilter()
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext context)
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeFilterPipelineAsync()
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeAsync()
Microsoft.AspNetCore.Builder.RouterMiddleware.Invoke(HttpContext httpContext)
Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

1 个答案:

答案 0 :(得分:1)

希望这对您有所帮助。从[BindProperty]中删除CUser并在[BindProperty]上添加Prefer见下文

public CUser User{ get; set; }
public CStudent Stu{ get; set; }
[BindProperty]
public CPreference Prefer { get; set; }