C#MVC允许更新重新编辑的字段

C#MVC允许更新重新编辑的字段,c#,asp.net-mvc,C#,Asp.net Mvc,我在mvc视图中有一个表单,其中包含名称、用户名、电子邮件地址、分支机构和部门的下拉列表。有两个存储过程正在检查重复的用户名和电子邮件地址。如果用户单击更新按钮,将显示错误 情景1 如果用户删除用户名并输入唯一的用户名,则表单将不会提交,因为数据库中仍然存在电子邮件地址 如果用户返回原始用户名,我如何允许更新继续 [HttpPost] public async Task<IActionResult> Update(ApplicationUserModel mo

我在mvc视图中有一个表单,其中包含名称、用户名、电子邮件地址、分支机构和部门的下拉列表。有两个存储过程正在检查重复的用户名和电子邮件地址。如果用户单击更新按钮,将显示错误

情景1

  • 如果用户删除用户名并输入唯一的用户名,则表单将不会提交,因为数据库中仍然存在电子邮件地址
  • 如果用户返回原始用户名,我如何允许更新继续

            [HttpPost]
        public async Task<IActionResult> Update(ApplicationUserModel model)
        {
            var appmodel = new ApplicationUserModel();
            appmodel.UserDetails = await ClaimsService.GetApplicationUserByID(model.Users.UserID);
            appmodel.Users = await ClaimsService.GetUserNameByID(model.Users.UserName);
            appmodel.User = await ClaimsService.GetEmailAddressIfExists(model.Users.EmailAddress);
            if (!appmodel.UserDetails.Name.Equals(model.Users.Name) || appmodel.UserDetails.BranchID != model.Users.BranchID || appmodel.UserDetails.DepartmentID != model.Users.DepartmentID )
            {
                if(!appmodel.UserDetails.UserName.Equals(model.Users.UserName) || !appmodel.UserDetails.EmailAddress.Equals(model.Users.EmailAddress))
                {
                    model.UserNameErrorMessage = "Username already exists";
                    model.EmailAddressErrorMessage = "Email address already exists";
                    model.Username = appmodel.Users.UserName;
                    model.EmailAddress = appmodel.User.EmailAddress;
                    model.Users = new Model.Applications.Tables.tblUsers() { Archive_User = "0", StatusID = 1 };
                    model.BranchSelectList = new SelectList(await BranchServices.GetBranchByCompanyID(1), "BranchID", "BranchFullName");
                    model.DepartmentSelectList = new SelectList(await DepartmentService.GetAllActiveDepartments(1), "DepartmentID", "Department");
                    return View(model);
                }
            }
            await ClaimsService.UpdateUserAsync(model.Users);
            string redirectUrl = string.Format("/ApplicationUsers/Users");
            return RedirectToAction("Message", "Home", new { type = Service.Utils.StringHelper.Types.UpdateSuccess, url = redirectUrl });
        }
    
    [HttpPost]
    公共异步任务更新(ApplicationUserModel模型)
    {
    var appmodel=new ApplicationUserModel();
    appmodel.UserDetails=await ClaimsService.GetApplicationUserByID(model.Users.UserID);
    appmodel.Users=await ClaimsService.GetUserNameByID(model.Users.UserName);
    appmodel.User=await ClaimsService.GetEmailAddressIfExists(model.Users.EmailAddress);
    如果(!appmodel.UserDetails.Name.Equals(model.Users.Name)| | appmodel.UserDetails.BranchID!=model.UserDetails.BranchID | | appmodel.UserDetails.DepartmentID!=model.Users.DepartmentID)
    {
    如果(!appmodel.UserDetails.UserName.Equals(model.Users.UserName)| |!appmodel.UserDetails.EmailAddress.Equals(model.Users.EmailAddress))
    {
    model.usernamererrormessage=“用户名已存在”;
    model.EmailAddressErrorMessage=“电子邮件地址已存在”;
    model.Username=appmodel.Users.Username;
    model.EmailAddress=appmodel.User.EmailAddress;
    model.Users=new model.Applications.Tables.tblUsers(){Archive_User=“0”,StatusID=1};
    model.BranchSelectList=new SelectList(等待BranchServices.GetBranchByCompanyID(1),“branchhid”,“BranchFullName”);
    model.DepartmentSelectList=新的SelectList(等待DepartmentService.GetAllActiveDepartments(1),“DepartmentID”,“Department”);
    返回视图(模型);
    }
    }
    wait ClaimsService.UpdateUserAsync(model.Users);
    字符串重定向URL=string.Format(“/ApplicationUsers/Users”);
    返回RedirectToAction(“消息”,“主页”,新{type=Service.Utils.StringHelper.Types.UpdateSuccess,url=redirectUrl});
    }
    
    将旧值(仅用户名和电子邮件)与新值进行比较,如果它们相同,则不检查以前是否存在。如果它们不同,则检查新的是否唯一

    如果电子邮件是'user1@example.com,username='user1'

    然后他输入了电子邮件user1@eample.com'但用户名='user1updated'

    您首先检查旧电子邮件和新电子邮件(两者都是'user1@example’)那就好了。 然后用新用户名检查旧用户名(此处不同),因此我检查数据库中是否存在新用户名(“user1updated”),如果没有,我会更新,否则不更新
     [HttpPost]
        public async Task<IActionResult> Update(ApplicationUserModel model)
        {
            string redirectUrl;
            var appmodel = new ApplicationUserModel();
            appmodel.UserDetails = await ClaimsService.GetApplicationUserByID(model.Users.UserID);
            appmodel.Users = await ClaimsService.GetUserNameByID(model.Users.UserName);
            appmodel.User = await ClaimsService.GetEmailAddressIfExists(model.Users.EmailAddress);
            if(appmodel.UserDetails.EmailAddress == model.Users.EmailAddress || appmodel.UserDetails.UserName == model.Users.UserName)
            {
                if (!appmodel.UserDetails.Name.Equals(model.Users.Name) || appmodel.UserDetails.BranchID != model.Users.BranchID || appmodel.UserDetails.DepartmentID != model.Users.DepartmentID)
                {
                    await ClaimsService.UpdateUserAsync(model.Users);
                    redirectUrl = string.Format("/ApplicationUsers/Users");
                    return RedirectToAction("Message", "Home", new { type = Service.Utils.StringHelper.Types.UpdateSuccess, url = redirectUrl });
                }
                else if (appmodel.UserDetails.EmailAddress != model.Users.EmailAddress || appmodel.UserDetails.UserName != model.Users.UserName)
                {
                    if (!appmodel.UserDetails.Name.Equals(model.Users.Name) || appmodel.UserDetails.BranchID != model.Users.BranchID || appmodel.UserDetails.DepartmentID != model.Users.DepartmentID)
                    {
                        await ClaimsService.UpdateUserAsync(model.Users);
                        redirectUrl = string.Format("/ApplicationUsers/Users");
                        return RedirectToAction("Message", "Home", new { type = Service.Utils.StringHelper.Types.UpdateSuccess, url = redirectUrl });
                    }
    
                    if(appmodel.UserDetails.EmailAddress == model.Users.EmailAddress)
                    {
                        model.EmailAddressErrorMessage = "";
                    }
                    if(appmodel.UserDetails.EmailAddress != model.Users.EmailAddress)
                    {
                        model.EmailAddressErrorMessage = "Email address already exists";
                    }
                    if(appmodel.UserDetails.UserName == model.Users.UserName)
                    {
                        model.UserNameErrorMessage = "";
                    }
                    if (appmodel.UserDetails.UserName != model.Users.UserName)
                    {
                        model.UserNameErrorMessage = "Username already exists";
                    }
                    model.Username = appmodel.Users.UserName;
                    model.EmailAddress = appmodel.User.EmailAddress;
                    model.Users = new Model.Applications.Tables.tblUsers() { Archive_User = "0", StatusID = 1 };
                    model.BranchSelectList = new SelectList(await BranchServices.GetBranchByCompanyID(1), "BranchID", "BranchFullName");
                    model.DepartmentSelectList = new SelectList(await DepartmentService.GetAllActiveDepartments(1), "DepartmentID", "Department");
                    return View(model);
                }
                await ClaimsService.UpdateUserAsync(model.Users);
                redirectUrl = string.Format("/ApplicationUsers/Users");
                return RedirectToAction("Message", "Home", new { type = Service.Utils.StringHelper.Types.UpdateSuccess, url = redirectUrl });
            }
    
    
            if (appmodel.UserDetails.EmailAddress == model.Users.EmailAddress)
            {
                model.EmailAddressErrorMessage = "";
            }
            if (appmodel.UserDetails.EmailAddress != model.Users.EmailAddress)
            {
                model.EmailAddressErrorMessage = "Email address already exists";
            }
            if (appmodel.UserDetails.UserName == model.Users.UserName)
            {
                model.UserNameErrorMessage = "";
            }
            if (appmodel.UserDetails.UserName != model.Users.UserName)
            {
                model.UserNameErrorMessage = "Username already exists";
            }
            model.Username = appmodel.Users.UserName;
            model.EmailAddress = appmodel.User.EmailAddress;
            model.Users = new Model.Applications.Tables.tblUsers() { Archive_User = "0", StatusID = 1 };
            model.BranchSelectList = new SelectList(await BranchServices.GetBranchByCompanyID(1), "BranchID", "BranchFullName");
            model.DepartmentSelectList = new SelectList(await DepartmentService.GetAllActiveDepartments(1), "DepartmentID", "Department");
            return View(model);
    
        }
    
    公共异步任务更新(ApplicationUserModel模型) { 字符串重定向URL; var appmodel=new ApplicationUserModel(); appmodel.UserDetails=await ClaimsService.GetApplicationUserByID(model.Users.UserID); appmodel.Users=await ClaimsService.GetUserNameByID(model.Users.UserName); appmodel.User=await ClaimsService.GetEmailAddressIfExists(model.Users.EmailAddress); if(appmodel.UserDetails.EmailAddress==model.Users.EmailAddress | | appmodel.UserDetails.UserName==model.Users.UserName) { 如果(!appmodel.UserDetails.Name.Equals(model.Users.Name)| | appmodel.UserDetails.BranchID!=model.UserDetails.BranchID | | appmodel.UserDetails.DepartmentID!=model.Users.DepartmentID) { wait ClaimsService.UpdateUserAsync(model.Users); 重定向URL=string.Format(“/ApplicationUsers/Users”); 返回RedirectToAction(“消息”,“主页”,新{type=Service.Utils.StringHelper.Types.UpdateSuccess,url=redirectUrl}); } else if(appmodel.UserDetails.EmailAddress!=model.Users.EmailAddress | | appmodel.UserDetails.UserName!=model.Users.UserName) { 如果(!appmodel.UserDetails.Name.Equals(model.Users.Name)| | appmodel.UserDetails.BranchID!=model.UserDetails.BranchID | | appmodel.UserDetails.DepartmentID!=model.Users.DepartmentID) { wait ClaimsService.UpdateUserAsync(model.Users); 重定向URL=string.Format(“/ApplicationUsers/Users”); 返回RedirectToAction(“消息”,“主页”,新{type=Service.Utils.StringHelper.Types.UpdateSuccess,url=redirectUrl}); } if(appmodel.UserDetails.EmailAddress==model.Users.EmailAddress) { model.EmailAddressErrorMessage=“”; } if(appmodel.UserDetails.EmailAddress!=model.Users.EmailAddress) { model.EmailAddressErrorMessage=“电子邮件地址已存在”; } if(appmodel.UserDetails.UserName==model.Users.UserName) { model.UserNameErrorMessage=“”; } if(appmodel.UserDetails.UserName!=model.Users.UserName) { model.usernamererrormessage=“用户名已存在”; } model.Username=appmodel.Users.Username; model.EmailAddress=appmodel.User.EmailAddress; model.Users=new model.Applications.Tables.tblUsers(){Archive_User=“0”,StatusID=1}; model.BranchSelectList=new SelectList(等待BranchServices.GetBranchByCompanyID(1),“branchhid”,“BranchFullName”); model.DepartmentSelectList=新的SelectList(等待DepartmentService.GetAllActiveDepartments(1),“DepartmentID”,“Department”); 返回视图(模型);