How to rendering Role model in View Page?
My Controller works good.
// POST: /Roles/Create [HttpPost] public async Task<IActionResult> Create(IdentityRole roleViewModel) { if (ModelState.IsValid) { if (!_roleManager.RoleExistsAsync("Admin").Result) { var newRole = new Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityRole(); newRole = roleViewModel; // newRole.Name = "Admin"; await _roleManager.CreateAsync(newRole); } } return View(); }
But, My View can't get "Name" Property.
@model IEnumerable<Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityRole> @{ ViewData["Title"] = "Create"; }<h2>Create</h2><form asp-action="Create"><div class="form-horizontal"><h4>Role</h4><hr /><div asp-validation-summary="ModelOnly" class="text-danger"></div><div class="form-group"><label asp-for="Name" class="col-md-2 control-label"></label><div class="col-md-10"><input asp-for="Name" class="form-control" /><span asp-validation-for="Name" class="text-danger"></span></div></div><div class="form-group"><div class="col-md-offset-2 col-md-10"><input type="submit" value="Create" class="btn btn-default" /></div></div></div></form><div><a asp-action="Index">Back to List</a></div> @section Scripts { @{await Html.RenderPartialAsync("_ValidationScriptsPartial");} }
The erros message is
"
'IEnumerable<IdentityRole>' does not contain a definition for 'Name' and no extension method 'Name' accepting a first argument of type 'IEnumerable<IdentityRole>' could be found (are you missing a using directive or an assembly reference?)"
So I suppose the View can't fetch RoleManaer's Model.
Did anybody ever have solved this issue?
p.s> The index view works good with Model
@model IEnumerable<Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityRole> @{ ViewData["Title"] = "Index"; }<h2>Index</h2><table class="table"><thead><tr><th> @Html.DisplayNameFor(model => model.Name)</th><th></th></tr></thead><tbody> @foreach (var item in Model) {<tr><td> @Html.DisplayFor(modelItem => item.Name)</td><td><a asp-action="Edit" asp-route-id="@item.Id">Edit</a> |<a asp-action="Details" asp-route-id="@item.Id">Details</a> |<a asp-action="Delete" asp-route-id="@item.Id">Delete</a></td></tr> }</tbody></table><p><a asp-action="Create">New</a></p>