this error appears when I try to access the user's profile logged by the layout. I want to see the details of a specific user, edit it or delete it so I need to send the id to the view, I am trying to do it using this function in the controller:
public IActionResult PerfilSocio( HttpContext contexto,int? id)
{
int x = Convert.ToInt32(contexto.Session.GetInt32("UserId"));
if (id == x)
{
foreach (var item in _context.Socios)
{
if (item.Idsocio == x)
{
var y = item;
return View(y);
}
}
}
else
{
return View("NaoEncontrado");
}
return View();
}
In the view I have this:
@model WebApplication1.Models.Socios
@{
ViewData["Title"] = "PerfilSocio";
}
<style>
body {
padding-top: 0px;
background-color: gray;
background-image: url();
background-image: url();
background-repeat: no-repeat;
}
</style>
<h1 style="color:white;"> <b>LabGym</b> </h1>
<p style="text-align:right;color:white;">
<i class="material-icons">✍</i>
<a href="~/Mensagems/VerMensagem"> Mensagens </a>
</p>
<br />
<div>
<hr />
<dl class="row">
<dt class="col-sm-2">
@*@Html.DisplayNameFor(model => model.Fotografia)*@
</dt>
<dd class="col-sm-10">
<img src="~/Fotos/ + model.Fotografia " />
</dd>
<dt class="col-sm-2">
@Html.DisplayNameFor(model => model.Email)
</dt>
<dd class="col-sm-10">
@Html.DisplayFor(model => model.Email)
</dd>
<dt class="col-sm-2">
@Html.DisplayNameFor(model => model.Telefone)
</dt>
<dd class="col-sm-10">
@Html.DisplayFor(model => model.Telefone)
</dd>
<dt class="col-sm-2">
@Html.DisplayNameFor(model => model.Sexo)
</dt>
<dd class="col-sm-10">
@*@Html.DisplayFor(model => model.Sexo)*@
@if (@Html.DisplayNameFor(model => model.Sexo) == "0")
{
<p>Feminino</p>
}
else
{
<p>Masculino</p>
}
</dd>
<dt class="col-sm-2">
@Html.DisplayNameFor(model => model.Altura)
</dt>
<dd class="col-sm-10">
@Html.DisplayFor(model => model.Altura)
</dd>
<dt class="col-sm-2">
@Html.DisplayNameFor(model => model.NomeUtilizador)
</dt>
<dd class="col-sm-10">
@Html.DisplayFor(model => model.NomeUtilizador)
</dd>
<dt class="col-sm-2">
@Html.DisplayNameFor(model => model.PesoInicial)
</dt>
<dd class="col-sm-10">
@Html.DisplayFor(model => model.PesoInicial)
</dd>
<dt class="col-sm-2">
@Html.DisplayNameFor(model => model.Estado)
</dt>
<dd class="col-sm-10">
@Html.DisplayFor(model => model.Estado)
</dd>
</dl>
</div>
<div>
<a asp-action="Edit" asp-route-id="@Model.Idsocio">Editar perfil</a> |
</div>
<br />
<br />
<br />
<br />
Can someone help me correct this error?
InvalidOperationException: Could not create an instance of type 'Microsoft.AspNetCore.Http.HttpContext'. Model bound complex types must not be abstract or value types and must have a parameterless constructor. Alternatively, give the 'contexto' parameter a non-null default value.