我已经使用了Asp.Net帐户注册控制器和查看模型,并尝试更改它们以执行以下操作。在用户注册其帐户后,他被重定向到具有两个复选框的下一页,他将不得不选择他是否想要成为卖家或买家。提交后,他将在数据库中分配一个号码。这是我到目前为止的代码:
视图模型
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");
}
答案 0 :(得分:0)
我的猜测是路由参数,您传递的内容和预期的模型之间存在类型不匹配。
在Register
中,您将Guid
Id
的匿名对象传递给Choice
。但是Choice
期望模型需要RegisterViewModel
类型和string
Id
。
解决此问题的一种方法是将Choice
签名更改为
Choice(RegisterViewModel model, Guid Id)
您使用
致电RedirectToAction
RedirectToAction("Choice", "RoleSelection", new {model, Id = Guid.NewGuid()})