2017-09-07 1 views
1

Ich versuche, ein Bearbeitungsformular für IdentityRole zu machen. Wenn die Seite geladen wird, erhalte ich normalerweise das Modell mit der ID "09e43076-c333-4145-bccb-8bc7f9db470e", aber wenn die Ansicht erstellt wird, sehe ich, dass @ Html.HiddenFor (m => m.Id) erhält einen alten Namen statt Id (siehe Screenshots in den Links unten). Warum passiert es? Vielen Dank!Warum IdentityRole-ID von alten Namen in Sicht überschrieben wird

GET: Edit

[HttpGet] 
    public ActionResult Edit(string id) 
    { 
     _context = new ApplicationDbContext(); 
     if (User.Identity.IsAuthenticated) 
     { 
      if (!isAdminUser()) 
      { 
       return RedirectToAction("Login", "Account"); 
      } 
     } 
     else 
     { 
      return RedirectToAction("Login", "Account"); 
     } 
     var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(new ApplicationDbContext())); 

     var role = roleManager.FindByName(id); 
     return View(role); 
    } 

Ausblick: Bearbeiten

@model Microsoft.AspNet.Identity.EntityFramework.IdentityRole 

@{ 
    ViewBag.Title = "Edit Role"; 
} 

<h2>Edit User Role:</h2> 
@section RolesCSS { 
    <link href="@Url.Content("~/Views/Role/style/style.css")" rel="stylesheet"/> 

    <div class="form-group"> 

     @using (Html.BeginForm("Edit", "Role", FormMethod.Post, new { @class = "AddRoleForm" })) 
     { 
      @Html.HiddenFor(m => m.Id) 
      @Html.HiddenFor(m => m.Users) 
      <div class="Label"> 
       <p>Edit New User Role Name:</p> 
      </div> 
      <div class="FormControl"> 
       @Html.EditorFor(m => m.Name, new {htmlAttributes = new {@class = "form-control"}}) 
      </div> 
      <div class="FormControl"> 
       <input class="btn btn-default" type="submit" value="Edit Role"/> 
      </div> 
     } 
    </div> 
} 

screenshot of values received as model in the view

screenshot of the source of the rendered html

Antwort

0

Obwohl ich eine Lösung gefunden habe, bin ich immer noch interessiert, was mit dem ersten Ansatz falsch war.

ich eine Abhilfe here (der Inhalt/Erklärung in der russischen lang ist) gefunden habe ich eine Klasse EditRoleModel als temporäres Modell erstellt, das Problem zu vermeiden ich vor,

public class EditRoleModel 
{ 

     public string Id { get; set; } 
     public string Name { get; set; } 

} 

mit war, dass ich gehe als Modell Ansicht:

[HttpGet] 
    public ActionResult Edit(string id) 
    { 
     _context = new ApplicationDbContext(); 
     if (User.Identity.IsAuthenticated) 
     { 
      if (!isAdminUser()) 
      { 
       return RedirectToAction("Login", "Account"); 
      } 
     } 
     else 
     { 
      return RedirectToAction("Login", "Account"); 
     } 
     var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(new ApplicationDbContext())); 

     var role = roleManager.FindByName(id); 
     var model = new EditRoleModel() {Id = role.Id, Name = role.Name}; 
     return View(model); 
    } 

dann leicht ich die Ansicht ändert Modelle vom Typ EditRoleModel zu erhalten:

@model WebUI.Models.EditRoleModel 

@{ 
    ViewBag.Title = "Edit Role"; 
} 

<h2>Edit User Role:</h2> 
@section RolesCSS { 
    <link href="@Url.Content("~/Views/Role/style/style.css")" rel="stylesheet"/> 

    <div class="form-group"> 

     @using (Html.BeginForm("Edit", "Role", FormMethod.Post, new { @class = "AddRoleForm" })) 
     { 
      <div>@Model.Id</div> 
      @Html.HiddenFor(m => m.Id) 
      <div class="Label"> 
       <p>Edit New User Role Name:</p> 
      </div> 
      <div class="FormControl"> 
       @Html.EditorFor(m => m.Name, new {htmlAttributes = new {@class = "form-control"}}) 
      </div> 
      <div class="FormControl"> 
       <input class="btn btn-default" type="submit" value="Edit Role"/> 
      </div> 
     } 
    </div> 
} 

und finaly ist dies der Controller, wo ich die IdentityRole aktualisieren:

[HttpPost] 
    public ActionResult Edit(EditRoleModel model) 
    { 
     _context = new ApplicationDbContext(); 
     if (User.Identity.IsAuthenticated) 
     { 
      if (!isAdminUser()) 
      { 
       return RedirectToAction("Login", "Account"); 
      } 
     } 
     else 
     { 
      return RedirectToAction("Login", "Account"); 
     } 
     var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(new ApplicationDbContext())); 

     IdentityRole role = roleManager.FindByName(model.Id); 
     if (role != null) 
     { 
      role.Name= model.Name; 
      IdentityResult result = roleManager.Update(role); 
      if (result.Succeeded) 
      { 
       return RedirectToAction("Index"); 
      } 
      else 
      { 
       ModelState.AddModelError("", "Something went wrong!"); 
      } 
     } 
     return View(model); 
    } 
Verwandte Themen