在IdentityFramework中,我为数据库添加新字段,即标准表Role(Description)
当我添加新角色时,我想获得对该角色的描述。
我制作了cshtml文件
SELECT rownum = DENSE_RANK() OVER (ORDER BY Number), Number
FROM #SEQNUMBERSTEMP
}
在控制器中发布查询
@using (Html.BeginForm("CreateRole", "Role", FormMethod.Post))
{
@Html.AntiForgeryToken()
<div id="create-role-box">
@Html.ValidationSummary(false)
<div class="form-group">
<label class="create-role-color">Название роли:</label>
@Html.TextBoxFor(x => x.Name, new { @class = "input-form-create-role create-role-color" })
@Html.ValidationMessageFor(x => x.Name)
</div>
<div class="form-group">
<label class="create-role-color">Описание роли:</label>
@Html.TextBoxFor(x => x.Description, new { @class = "input-form-create-role create-role-color" })
@Html.ValidationMessageFor(x => x.Description)
</div>
<div class="form-group" style="margin: 22px 0px 0px 0px;">
<button type="submit" class="button-create-role">Добавить</button>
@Html.ActionLink("Отмена", "Index", null, new { @class = "button-cancel" })
</div>
</div>
我在存储库中描述的 public async Task<ActionResult> CreateRole([Required]string name, string description)
{
if (ModelState.IsValid)
{
IdentityResult result = await roleManager.CreateRoleAsync(name, description);
if (result.Succeeded)
{
return RedirectToAction("Index");
}
else
{
AddErrorsFromResult(result);
}
}
return View(name);
}
方法
CreateRoleAsync(name, description);
我的 public async Task<IdentityResult> CreateRoleAsync(string name, string description)
{
IdentityResult result = await userRole.CreateAsync(new UserRole(name, description));
return result;
}
模型是
UserRole
,如果需要我的上下文
public class UserRole : IdentityRole
{
public UserRole(string name, string description) : base(name)
{
description = Description;
}
public string Description { get; set; }
}
问题在于public class IdentityUserContext : IdentityDbContext<UserModel>
{
public IdentityUserContext() : base("UserIdentityDbContext")
{
}
// creating method realize by ninject in Ninject.Web.Common.cs WebUI->App_Start
}
未保存在数据库中,
它为空,但是该方法采用description
的值,为什么id不保存在数据库中。
答案 0 :(得分:0)
首先将UserRole
类重命名为ApplicationRole
,因为UserRole
令人困惑。 UserRole
应该为“用户和角色多对多联接”表命名。
public class ApplicationRole : IdentityRole
{
public ApplicationRole(string name) : base(name){}
public string Description { get; set; }
}
现在创建一个RoleViewModel
,如下所示:
public class RoleViewModel
{
public string Id { get; set; }
[Required(AllowEmptyStrings = false)]
[Display(Name = "RoleName")]
public string Name { get; set; }
public string Description { get; set; }
}
然后查看您的Create
视图:
@model Domain.ViewModels.RoleViewModel
@using (Html.BeginForm())
{
// Your input fields and submit button here
}
现在编写您的CreateRole
Post方法,如下所示:
[HttpPost]
public async Task<ActionResult> CreateRole(RoleViewModel roleViewModel)
{
if (ModelState.IsValid)
{
var role = new ApplicationRole(roleViewModel.Name);
role.Description = roleViewModel.Description; // <--- Here you have to assign the `Description` value
var roleresult = await RoleManager.CreateAsync(role);
if (!roleresult.Succeeded)
{
ModelState.AddModelError("", roleresult.Errors.First());
return View();
}
return RedirectToAction("Index");
}
return View();
}
现在一切正常!
如果要通过存储库方法创建角色,请按如下所示编写CreateRoleAsync
存储库方法:
public async Task<IdentityResult> CreateRoleAsync(ApplicationRole applicationRole)
{
IdentityResult result = await userRole.CreateAsync(applicationRole);
return result;
}
然后按如下所示在控制器方法中调用它:
[HttpPost]
public async Task<ActionResult> CreateRole(RoleViewModel roleViewModel)
{
if (ModelState.IsValid)
{
var role = new ApplicationRole(roleViewModel.Name);
role.Description = roleViewModel.Description; // <--- Here you have assign the Description value
IdentityResult result = await roleManager.CreateRoleAsync(role); // <-- call your repository method here.
if (!roleresult.Succeeded)
{
ModelState.AddModelError("", roleresult.Errors.First());
return View();
}
return RedirectToAction("Index");
}
return View();
}