【发布时间】:2017-05-14 04:59:28
【问题描述】:
我目前正在处理一个 MVC 项目,该项目需要为顾问和客户提供 CRUD(2 个单独的表)。一开始我们只是为这些用户创建了 CRUD,现在意识到我们需要为此添加注册和登录功能,有什么方法可以使用我们生成的现有控制器调用内置的 AccountController?
或者有没有办法在单个控制器或视图中调用两个或多个模型?
客户端模型
public class Client:ApplicationUser
{
[Key]
[Required]
public int Id { get; set; }
[Required]
[Display(Name = "Client Name")]
public string ClientName { get; set; }
[Required]
[Display(Name = "Client Address")]
public string ClientAddress { get; set; }
[Required]
[Display(Name = "Contact Number")]
public long ContactNumber { get; set; }
[Required]
[EmailAddress]
[Display(Name = "Email")]
public string Email { get; set; }
[Required]
[Display(Name = "Project Leader")]
public string ProjectLeader { get; set; }
public virtual ManageTravel ManageTravel { get; set; }
[Display(Name = "Rate")]
public double Rate { get; set; }
[Display(Name = "Distance")]
public string Distance { get; set; }
[Display(Name = "Rate")]
[Required]
public string TravelCode { get; set; }
顾问模特
public class Consultant:ApplicationUser
{
[Key]
public int ConsultantNum { get; set; }
[Required]
[Display(Name = "First Name")]
public string FirstName { get; set; }
[Required]
[Display(Name = "Last Name")]
public string LastName { get; set; }
[Required]
[Display(Name = "Contact Number")]
public int ContactNumber { get; set; }
[Required]
[Display(Name = "Consultant Address")]
public string ConsultantAddress { get; set; }
[Required]
[Display(Name = "Email")]
[EmailAddress]
public string Email { get; set; }
[Required]
[Display(Name = "Consultant Type")]
public string ConsultantType { get; set; }
[Required]
[Display(Name = "Commission Code")]
public string ComissionCode { get; set; }
[Required]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
[Required]
[Display(Name = "Role Type")]
public string RoleType { get; set; }
}
帐户控制器
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Register(RegisterViewModel model)
{
if (ModelState.IsValid)
{
var user = new ApplicationUser { UserName = model.Email, Email = model.Email };
var result = await UserManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
// This code has been added to the action for email confirmation
var code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
var callbackUrl = Url.Action(
"ConfirmEmail",
"Account",
new { userId = user.Id, code = code },
protocol: Request.Url.Scheme);
await UserManager.SendEmailAsync(
user.Id,
"Confirm your account",
"Please confirm your account by clicking this link: <a href=\""
+ callbackUrl + "\">link</a>");
ViewBag.Link = callbackUrl;
return View("DisplayEmail"); // DisplayEmail View has been created
}
AddErrors(result);
//end of email confirmation code
}
// If we got this far, something failed, redisplay form
return View(model);
}
【问题讨论】:
标签: c# html asp.net-mvc model-view-controller