C
C#17mo ago
NDOT

❔ MVC error when i call a property from the model

see below
10 Replies
NDOT
NDOT17mo ago
i just have a model >
public class HomeModel
{
public string Name { get; set; }
public string Email { get; set; }

}
public class HomeModel
{
public string Name { get; set; }
public string Email { get; set; }

}
that's called on the main controller >
public IActionResult Index()
{
HomeModel home = new HomeModel();
home.Email = "nrochalacerda@gmail.com";
home.Name= "Nicolas Rocha Lacerda";
return View(home);
}
public IActionResult Index()
{
HomeModel home = new HomeModel();
home.Email = "nrochalacerda@gmail.com";
home.Name= "Nicolas Rocha Lacerda";
return View(home);
}
NDOT
NDOT17mo ago
but when i call it on the view, it returns me that's null
добряк
добряк17mo ago
Could you share the Home .cshtml code?
NDOT
NDOT17mo ago
<!DOCTYPE html>
@model HomeModel
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>@ViewData["Title"] - ManageSchool</title>
<link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.min.css" />
<link rel="stylesheet" href="~/css/site.css" asp-append-version="true" />
<link rel="stylesheet" href="~/ManageSchool.styles.css" asp-append-version="true" />
</head>
<body>
<header>
<nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-light border-bottom box-shadow mb-3 bg-success text-white-50">
<div class="container-fluid">
<div class="navbar-collapse collapse d-sm-inline-flex justify-content-between">
<ul class="navbar-nav flex-grow-1">
<li class="nav-item">
<a class="nav-link text-white" asp-area="" asp-controller="Home" asp-action="Materias">Materias</a>
</li>
</ul>
<span class="d-flex justify-content-end text-white"> Bem vindo novamente <strong> @Model.Name</strong></span>
</div>
</div>
</nav>
</header>
<div class="container">
<main role="main" class="pb-3">
@RenderBody()
</main>
</div>
@await RenderSectionAsync("Scripts", required: false)
</body>
</html>
<!DOCTYPE html>
@model HomeModel
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>@ViewData["Title"] - ManageSchool</title>
<link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.min.css" />
<link rel="stylesheet" href="~/css/site.css" asp-append-version="true" />
<link rel="stylesheet" href="~/ManageSchool.styles.css" asp-append-version="true" />
</head>
<body>
<header>
<nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-light border-bottom box-shadow mb-3 bg-success text-white-50">
<div class="container-fluid">
<div class="navbar-collapse collapse d-sm-inline-flex justify-content-between">
<ul class="navbar-nav flex-grow-1">
<li class="nav-item">
<a class="nav-link text-white" asp-area="" asp-controller="Home" asp-action="Materias">Materias</a>
</li>
</ul>
<span class="d-flex justify-content-end text-white"> Bem vindo novamente <strong> @Model.Name</strong></span>
</div>
</div>
</nav>
</header>
<div class="container">
<main role="main" class="pb-3">
@RenderBody()
</main>
</div>
@await RenderSectionAsync("Scripts", required: false)
</body>
</html>
добряк
добряк17mo ago
this is the code from _Layout.cshtml, right? You inject model in the controller page, which is located in the Views/Home/Index.cshtml
Accord
Accord17mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
NDOT
NDOT16mo ago
exactly how can i do it ?
добряк
добряк16mo ago
all you need is to call the model in Views/Home/Index.cshtml instead of _Layout.cshtml and this will work.
добряк
добряк16mo ago
Layout in ASP.NET Core
Learn how to use common layouts, share directives, and run common code before rendering views in an ASP.NET Core app.
Accord
Accord16mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.