I am making an application in ASP.NET Core 2.1 MVC, in which I am using Identity for user authentication, I have a class called Folder which has a relation with ApplicationUser this IdentityUser deriba, the relationship is as follows Una Folder can belong to a User and a User can have many folders when creating a folder records everything well but when I want to edit that folder the first error I have is the Edit of Get where the id = 1 but when it arrives to the Edit of the Post that same id = 0 where it does not update and redirects me to a blank page, then the other error is that when I want to edit the folder and it has a relationship with Department, Province, Municipality I recover their values but not with Users.
// GET: Carpetass/Edit/5
public async Task<IActionResult> Edit(int? id)
{
if (id == null)
{
return NotFound();
}
var carpeta = await _context.Carpetas.FindAsync(id);
ViewData["DepartamentoId"] = new SelectList(_context.Departamentos, "DepartamentoId", "Nombre");
var fullNames = await _context.Users.ToListAsync();// Select(u => new { UserID = u.Id, FullName = u.Nombres + " " + u.Apellidos }).ToList();
//var fullNames = await _context.Users.ToListAsync();
**ViewData["Id"] = new SelectList(await _context.Users.ToListAsync(), "Id", "FullName");**
//ViewData["Id"] = _context.Users.Select(u => u.FullName).ToList();
ViewData["ProvinciaId"] = new SelectList(_context.Provincias, "ProvinciaId", "Nombre");
ViewData["MunicipioId"] = new SelectList(_context.Municipios, "MunicipioId", "Nombre");
ViewData["UbicacionId"] = new SelectList(_context.Ubicaciones, "UbicacionId", "Nombre");
//ViewData["Id"] = new SelectList(_context.Users, "Id", "FullName", resolucion.Id);
//ViewData["UsersId"] = new SelectList(_context.Users.ToList(), "Id", "FullName", carpeta.Id);
//ViewData["UsersId"] = new SelectList(_context.Users.ToList(), "Id", "FullName");
if (carpeta == null)
{
return NotFound();
}
return View(carpeta);
}
// POST: Carpetass/Edit/5
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(int id, [Bind("CarpetaId,IDCarpeta,AgrupacionSocial,Cuerpos,Fojas,Poligono,Id,DepartamentoId,ProvinciaId,MunicipioId,UbicacionId")] Carpeta carpeta)
{
if (id != carpeta.CarpetaId)
{
return NotFound();
}
if (ModelState.IsValid)
{
try
{
_context.Update(carpeta);
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!CarpetasExists(carpeta.CarpetaId))
{
return NotFound();
}
else
{
throw;
}
}
return RedirectToAction(nameof(Index));
}
return View(carpeta);
}
According to tests the problem is in this line of code in both cases.
ViewData ["Id"] = new SelectList (await _context.Users.ToListAsync (), "Id", "FullName");
First, it does not grab the user's Id and second, it changes the 0's in the post.