【问题标题】:ArgumentNullException: Value cannot be null. Parameter name: entityArgumentNullException:值不能为空。参数名称:实体
【发布时间】:2019-11-26 08:26:27
【问题描述】:

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

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

此处显示了 C# 代码。粉煤灰。

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:值不能为空。参数名称:实体

被抛出。正如我已经提到的,我可以从数据库中写入页面。

完整的堆栈:

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)

【问题讨论】:

  • 对!你建议我如何解决它?
  • 您的public CPreference Prefer { get; set; } 属性上可能缺少[BindProperty]?只是一个猜测,不过,我不知道那个框架 =)

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


【解决方案1】:

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

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

【讨论】:

  • 就是这样。非常感谢。
猜你喜欢
  • 2018-09-29
  • 2015-07-25
  • 2015-07-17
  • 1970-01-01
  • 2015-01-19
  • 2019-11-06
  • 1970-01-01
  • 2017-06-06
  • 1970-01-01
相关资源
最近更新 更多