如何在UserProfile中创建自定义附加字段

时间:2014-06-24 10:57:46

标签: c# asp.net-mvc asp.net-mvc-4 asp.net-membership asp.net-identity

我正在尝试创建自定义用户字段。我已加入注册表。一切正常,自动创建表格,但字段 City NULL。有人知道为什么会这样吗?

public class ApplicationUser : IdentityUser
{
    public string City { get; set; }
}

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    static ApplicationDbContext()
    {
        Database.SetInitializer(new MySqlInitializer());
    }
    public ApplicationDbContext() : base("MySql.Data.MySqlClient")
    {
    }
}

MySqlInitializer.cs

public class MySqlInitializer : IDatabaseInitializer<ApplicationDbContext>
{
    public void InitializeDatabase(ApplicationDbContext context)
    {
        if (!context.Database.Exists())
        {
            context.Database.Create();
        }
        else
        {
            var migrationHistoryTableExists = ((IObjectContextAdapter)context).ObjectContext.ExecuteStoreQuery<int>(
            string.Format(
              "SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = '{0}' AND table_name = '__MigrationHistory'",
              "[users]"));

            if (migrationHistoryTableExists.FirstOrDefault() == 0)
            {
                context.Database.Delete();
                context.Database.Create();
            }
        }
    }
}

Register.cshtml

<div class="form-group">
    @Html.LabelFor(m => m.City, new { @class = "col-md-2 control-label" })
    <div class="col-md-10">
        @Html.TextBoxFor(m => m.City, new { @class = "form-control" })
    </div>
</div>
已添加公共类RegisterViewModel 中的

AccountViewModels.cs

    [Required]
    [Display(Name = "City")]
    public string City { get; set; }

AccountController.cs

    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Register(RegisterViewModel model)
    {
        if (ModelState.IsValid)
        {
            var user = new ApplicationUser() { UserName = model.UserName };
            var result = await UserManager.CreateAsync(user, model.Password);
            if (result.Succeeded)
            {
                await SignInAsync(user, isPersistent: false);
                return RedirectToAction("Index", "Home");
            }
            else
            {
                AddErrors(result);
            }
        }

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

null

1 个答案:

答案 0 :(得分:2)

创建用户对象时需要包含City变量:

[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Register(RegisterViewModel model)
{
    if (ModelState.IsValid)
    {
        var user = new ApplicationUser() { UserName = model.UserName, City = model.City  };
        var result = await UserManager.CreateAsync(user, model.Password);
        if (result.Succeeded)
        {
            await SignInAsync(user, isPersistent: false);
            return RedirectToAction("Index", "Home");
        }
        else
        {
            AddErrors(result);
        }
    }

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

编辑:更新了使用代码的代码。