将参数传递给控制器时为Null对象

本文关键字:Null 对象 控制器 参数传递 | 更新日期: 2023-09-27 18:26:20

我已经使用了Asp.Net帐户注册控制器和视图模型,并尝试对它们进行一些更改以执行以下操作。用户注册帐户后,会被重定向到带有两个复选框的下一个页面,在那里他必须选择是想成为卖家还是买家。提交后,他将在数据库中分配一个号码。这是我迄今为止的代码:

ViewModel

public class RegisterViewModel
{
    [Required]
    [EmailAddress]
    [Display(Name = "Email")]
    public string Email { get; set; }
    [Required]
    [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 8)]
    [Display(Name = "UserName")]
    public string UserName { get; set; }
    [Required]
    [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
    [DataType(DataType.Password)]
    [Display(Name = "Password")]
    public string Password { get; set; }
    [DataType(DataType.Password)]
    [Display(Name = "Confirm password")]
    [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
    public string ConfirmPassword { get; set; }
    //[Required]
    //[Display(Name = "Date Of Birth")]
    public DateTimeOffset UserDOB { get; set; }
    public int? UserRole { get; set; }
    public AspNetUser AspNetUser { get; set; }
    public int Id { get; set; }
    public bool buyer { get; set; }
    public bool seller { get; set; }
}

这是我的注册管理员:

public async Task<ActionResult> Register(RegisterViewModel model, ApplicationUser test)
    {
        if (ModelState.IsValid)
        {
            var user = new ApplicationUser { UserName = model.UserName, Email = model.Email, UserDOB = model.UserDOB };
            var result = await UserManager.CreateAsync(user, model.Password);
            if (result.Succeeded)
            {
                await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false);
                return RedirectToAction("Choice", "RoleSelection", new { Id = Guid.NewGuid()});
            }
            AddErrors(result);
        }

        return View(model);
    }

现在我陷入了困境。代码不起作用。我认为持有用户信息的字符串Id参数或模型存在问题。我只想把这个值传递给数据表:

   public ActionResult Choice(string Id)
    {
        using (var db = new FoundationEntities())
        {
            var trans = db.AspNetUsers.Where(x => x.Email == User.Identity.Name).FirstOrDefault();
            return View(trans);
        }
    }

    [HttpPost]
    public ActionResult Choice(RegisterViewModel model, string Id)
    {
        using (var db = new FoundationEntities())
        {
            if (model.buyer == true && model.seller == false)
            {
            var work = db.AspNetUsers.Where(x => x.Email == User.Identity.Name).Select(x => x.UserRole);
                model.UserRole = 3;
                db.SaveChanges();
            }
            if (model.seller == true && model.buyer == false)
            {
                var work = db.AspNetUsers.Where(x => x.Email == User.Identity.Name).Select(x => x.UserRole);
                model.UserRole = 2;
                db.SaveChanges();
            }
        }
        return RedirectToAction("Index", "Home");
    }

将参数传递给控制器时为Null对象

我的猜测是,您所传递的路由参数和预期模型之间存在类型不匹配。

Register中,将具有Guid Id的匿名对象传递给Choice。但是Choice期望类型为RegisterViewModelstring的模型期望Id

一种解决方法是将Choice签名更改为

Choice(RegisterViewModel model, Guid Id)

以及您使用拨打RedirectToAction的电话

RedirectToAction("Choice", "RoleSelection", new {model, Id = Guid.NewGuid()})