MVC 5有两种注册表格

时间:2015-04-15 18:38:10

标签: asp.net-mvc asp.net-mvc-5

是否可以或甚至建议为同一个应用程序创建2个注册表单?我正在为此项目使用ASP.NET Identity 2.0。两种登记表格都有相似的字段名称,但目的不同。一个是教师培训,另一个是捐赠。创建两个单独的应用程序会更好吗?

示例首次登记表

[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Register1(RegisterViewModel model)
{
    if (ModelState.IsValid)
    {
          var user = new ApplicationUser 
           { 
            UserName = model.Email, 
            Email = model.Email 
              };

        // Add the Address properties:
        user.Address = model.Address;
        user.City = model.City;
        user.State = model.State;
        user.PostalCode = model.PostalCode;
        // Additional fields specific to Registration Form1
        field1
        field2
        .....

        var result = await UserManager.CreateAsync(user, model.Password);
        if (result.Succeeded)
        {
            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");
        }
        AddErrors(result);
    }

    // If we got this far, something failed, redisplay form
    return View(model);
}

第二次登记表

[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Register2(RegisterViewModel model)
     {
    if (ModelState.IsValid)
    {
          var user = new ApplicationUser 
           { 
            UserName = model.Email, 
            Email = model.Email 
              };

        // Add the Address properties:
        user.Address = model.Address;
        user.City = model.City;
        user.State = model.State;
        user.PostalCode = model.PostalCode;
        // Additional fields specific to Registration Form2
        field1
        field2
        SiteURL
        PaymentType

        var result = await UserManager.CreateAsync(user, model.Password);
        if (result.Succeeded)
        {
            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");
        }
        AddErrors(result);
    }

    // If we got this far, something failed, redisplay form
    return View(model);
}

1 个答案:

答案 0 :(得分:0)

  

是否可以或甚至建议为同一个应用程序创建2个注册表单?

,这是可能的。

  

创建两个单独的应用程序会更好吗?

没有需要创建单独的应用程序 - 只需为业务和数据库提供底层结构和逻辑,以便相应地处理表单的输入。