-2

I encounter null in the HTML section I think there is no problem. I really don't know why I get this error. I want to read projects from SQL and display them, and if nothing exists, show an empty table. Please help me.

[Route("admin/MyProject")]
public IActionResult MyProject()
{
    return View();
}

public IActionResult MyProject(Projects projects)
{
    var Project = new Projects()
    {
        Id = projects.Id,
        Title = projects.Title,
        Programming_lan = projects.Programming_lan,
        WebUrl = projects.WebUrl,
        ProjectPhoto = projects.ProjectPhoto,
    };
    return View();
}
public class Projects
{
    [Key]
    public int Id { get; set; }
    [Required]
    public string Title { get; set; }
    [Required]
    public string Programming_lan { get; set; }
    [Required]
    public string ProjectPhoto { get; set; }
    [Required]
    [DataType(DataType.Url)]
    public string WebUrl { get; set; }
}
<tbody>
    @foreach (var Projects in Model.Id.ToString())
    {
        <tr>
            <th scope="row">@Model.Title</th>
            <td>@Model.WebUrl</td>
            <td>@Model.Programming_lan</td>
            <td><img src="/img/@(Model.Id).jpg" alt="عکس پروژه" /></td>
            <td>
                <a asp-controller="home" asp-action="Edit" class="btn btn-outline-success">ویرایش</a>
                <a asp-controller="home" asp-action="Remove" class="btn btn-outline-danger" onclick="return confirm('آیا مطئمن هستید؟')">حذف</a>
            </td>
        </tr>
    }
</tbody>
Theodor Zoulias
  • 34,835
  • 7
  • 69
  • 104
  • 1
    I don't quite understand what you're trying to achieve here ... you have a foreach that's trying to obtain multiple objects from Model.Id.ToString()? That is only likely to produce one iteration. And I can't see anywhere in your code examples where the "Model" object/variable is declared - where is the code/declaration for "Model"? – Craig Aug 13 '23 at 23:08
  • 1
    Your foreach loop is the issue Model.Id is not a collection you need to remove .Id.ToString(). – AliK Aug 13 '23 at 23:29

0 Answers0