如何在AspNetUsers表

时间:2017-08-23 03:18:03

标签: asp.net customization asp.net-mvc-5.2

我是ASP.NET新手,但我已经为我的AspNetUsers数据库添加了FirstName(字符串),LastName(字符串)和AccountType(int,可以是0,1或2)。我在MVC 5工作。虽然我发现了很多关于更改密码的文章,但我想添加允许用户更改AccountType的功能。

我知道在这种情况下,角色可能是更好的选择,但我已经实施了很多,我现在不想改变它。

帐户类型更改的表单包含一个字段供用户输入他/她的密码(以确认他们希望帐户类型更改,以及下拉列表提供三个选项,其值为0,1或2。

简而言之,用户将输入密码,单击下拉列表中的选项,然后单击“sumbit按钮”,AccountType将更改为不同的int

@using SouthMeckNTHS.Models
@using SouthMeckNTHS.Extensions
@model ChangeAccountViewModel
@{
    ViewBag.Title = "Change Account Type";
}

<section class="engine"></section>
<section class="mbr-section article mbr-parallax-background mbr-after-navbar" id="msg-box8-7d" style="background-image: url(../assets/images/full-unsplash-photo-1438354886727-070458b3b5cf-2000x1553-39.jpg); padding-top: 120px; padding-bottom: 80px;">
<div class="mbr-overlay" style="opacity: 0.5; background-color: rgb(34, 34, 34);">
</div>
<div class="container">
    <div class="row">
        <div class="col-md-8 col-md-offset-2 text-xs-center">
            <h3 class="mbr-section-title display-2">MANAGE YOUR ACCOUNT</h3>
            <div class="lead"><p>Change your account settings</p></div>
        </div>
    </div>
</div>

@using (Html.BeginForm("AccountChange", "Manage", FormMethod.Post, new { 
@class = "form-horizontal", role = "form" }))
{
    Html.AntiForgeryToken();

<div class="mbr-section mbr-section__container mbr-section__container--middle">
    <div class="container">
        <div class="row">
            <div class="col-xs-12 text-xs-center">


            </div>
        </div>
    </div>
</div>
<div class="mbr-section mbr-section-nopadding">
    <div class="container">
        <div class="row">
            @Html.ValidationSummary(true, "", new { @class = "text-danger" })
            <div class="col-xs-12 col-lg-10 col-lg-offset-1">
                <div class="row row-sm-offset">
                    <div class="col-xs-12 col-md-12">
                        <div class="form-group">
                            <label class="form-control-label" for="form1-z-name">Enter your current email<span class="form-asterisk">*</span></label>
                            @Html.TextBoxFor(m => m.CurrentPassword, new { @class = "form-control" })
                            @Html.ValidationMessageFor(m => m.CurrentPassword, "", new { @class = "text-danger", @style = "color:white" })
                            <!--<input type="text" class="form-control" name="name" required="" data-form-field="Name" id="form1-z-name">-->
                        </div>
                    </div>
                    <div class="col-xs-12 col-md-12">
                        <div class="form-group">
                            <label class="form-control-label" for="form1-z-name">Choose Your Account Type<span class="form-asterisk">*</span></label>
                            @Html.DropDownListFor(
m => m.NewAccountType,
new SelectList(
new List<Object>
{
                                                      new { value = 0 , text 
= "Learner"  },
                                                      new { value = 1 , text 
= "Contributor" },
                                                      new { value = 2 , text 
= "Competitor"}
},
"value",
"text"
), new { @style = "border: 1px solid #e8e8e8;padding: 0.5em 1.07em 0.5em;background:  #f5f5f5;font-size: 0.875rem;border-radius: 5px;width: 100%;line-height: 1.43;min-height: 3.5em;" }
)
                        </div>
                    </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</section>
<section class="mbr-section mbr-section__container" id="buttons1-r" style="background-color: rgb(255, 255, 255); padding-top: 10px; padding-bottom: 5px;">
<div class="container">
    <div class="row">
        <div class="col-xs-12">
            <div class="text-xs-center"><input type="submit" class="btn btn-primary text-xs-center" value="CHANGE ACCOUNT TYPE" /> </div>
        </div>
    </div>
</div>
</section>
}

这是ManageViewModels.cs的一部分:

public class ChangeAccountViewModel
{

   [Required]
    [DataType(DataType.Password)]
    [Display(Name = "Password")]
    public string CurrentPassword { get; set; }

    [Required]
    [Display(Name = "Choose Account Type")]
    public int NewAccountType { get; set; }
}

因此,可能在ManageController.cs中,我应该添加什么来允许来自表单第一部分的信息检查用户的密码以及我应该将哪些内容添加到同一文件中以便(如果密码匹配)它将根据用户的选择更新AccountType数据库吗?

(“更改密码”功能的编辑副本不起作用)

更新 我将此添加到ManageController.cs:

    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> AccountChange(ChangeAccountViewModel model)
    {
        if (ModelState.IsValid)
        {
            // Get the current application user
            var user = await UserManager.FindByIdAsync(User.Identity.GetUserId());


            //Update the details
            user.AccountType = model.NewAccountType;


            // Update user address
            var result = await UserManager.UpdateAsync(user);




        }

        return View(model);
    }

但它只能运作一次。我运行它的测试,我更改了用户的帐户类型,并保存到AspNetUsers数据库。但是,当我再次运行它时,我无法在任何用户帐户中更改它。每当用户更改下拉列表并单击提交按钮时,我该怎么做才能使其工作?

1 个答案:

答案 0 :(得分:0)

我决定只是将用户注销并让他们重新登录。

    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> AccountChange(ChangeAccountViewModel model)
    {
        //THE THING THAT ONLY WORKED ONCE:
        if (ModelState.IsValid)
        {
            // Get the current application user
            var user = await UserManager.FindByIdAsync(User.Identity.GetUserId());


            //Update the details
            user.AccountType = model.NewAccountType;


            // Update user account type
            var result = await UserManager.UpdateAsync(user);

            if (result.Succeeded)
            {
                AuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie);
                return RedirectToAction("../Account/Login");
            }
        }
        return View(model);

    }