【问题标题】:System.ArgumentNullException: Value cannot be null ASP.NET MVC5System.ArgumentNullException:值不能为空 ASP.NET MVC5
【发布时间】:2020-09-25 21:34:00
【问题描述】:

我有一个贷款申请表,它被设置为自动将用户注册为ApplicationUser。 (它更像是注册控制器的扩展版本)。我已经调试了,但我仍然收到 System.ArgumentNullException: Value cannot be null 错误,它指向 BaseController (return base.BeginExecuteCore(callback, state))

基础控制器

public class BaseController : Controller
{
    #region Protected Methods

    protected override IAsyncResult BeginExecuteCore(AsyncCallback callback, object state)
    {
        string cultureName = RouteData.Values["culture"] as string;
        // Attempt to read the culture cookie from Request
        if (cultureName == null)
            cultureName = Request.UserLanguages != null && Request.UserLanguages.Length > 0 ? Request.UserLanguages[0] : null; // obtain it from HTTP header AcceptLanguages

        // Validate culture name
        cultureName = CultureUtility.GetImplementedCulture(cultureName);
        if (RouteData.Values["culture"] as string != cultureName)
        {
            // Force a valid culture in the URL
            RouteData.Values["culture"] = cultureName.ToLowerInvariant(); // lower case too
            Response.RedirectToRoute("Default", RouteData.Values); // Redirect user
        }
        // Modify current thread's cultures
        Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(cultureName);
        Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;
        return base.BeginExecuteCore(callback, state);
    }

    #endregion Protected Methods
}

内部CheckRateController继承自BaseController

// POST: CheckRate/Index
    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Index(CheckYourRateViewModel model)
    {
        if (ModelState.IsValid)
        {
            var user = new ApplicationUser
            {
                FirstName = model.FirstName,
                LastName = model.LastName,
                UserName = model.UserName,
                PhoneNumber = model.Phone,
                DateOfBirth = model.DateOfBirth,
                State = model.State,
                Street = model.Street,
                City = model.City,
                Country = model.Country,
                YearlyIncome = model.IndividualYearlyIncome,
                AdditionalYearlyIncome = model.AdditionalYearlyIncome,
                IsAdmin = YesNo.No,
                CashBalance = 0.00M,
                IsBorrower = YesNo.Yes,
                RegisteredDate = DateTime.Now,
                EmploymentStatus = model.EmploymentStatus
            };

            var result = await UserManager.CreateAsync(user, model.Password);
            if (result.Succeeded)
            {
                await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);

                return RedirectToAction("Index", "Home");
            }
            AddErrors(result);
        }

        return View(model);
    }

 private void AddErrors(IdentityResult result)
    {
        foreach (var error in result.Errors)
        {
            ModelState.AddModelError("", error);
        }
    }

这是错误截图:https://prnt.sc/sv0rx7

【问题讨论】:

    标签: c# asp.net-mvc asp.net-mvc-4 asp.net-mvc-5


    【解决方案1】:

    查看堆栈跟踪,这很明显:

    System.ComponentModel.DataAnnotation.ValidationContext.set_DisplayName(String Value)
    

    我认为可能发生的情况是,您使用的 DisplayAttribute 名称为空。

    ([DisplayAttribute(Name="")]
    

    这需要更改,因此它包含一些文本甚至只是间距。

    【讨论】:

      猜你喜欢
      • 2021-08-01
      • 2020-03-01
      • 2013-02-10
      • 1970-01-01
      • 2019-11-01
      • 1970-01-01
      • 2018-07-10
      • 2021-10-11
      • 1970-01-01
      相关资源
      最近更新 更多