Search code examples
asp.net-mvcasp.net-mvc-4razordata-bindingrazor-2

ASP.NET MVC 4 binding


I'm a beginner in ASP.NET MVC and i began a small project. I created CRUD methods in my controller and it works. I just have a problem with a method i created to see the details of a form (my project is to create forms with a backoffice). I'll show you the code:

The controller:

public ActionResult Detail(Guid id)
    {
        Form formDetail = null;

        using (var ctx = new FormsContext())
        {
            formDetail = ctx.Forms.Find(id);
        }

        return View(formDetail);
    }

The model:

public class Form
{
    public Guid FormId { get; set; }
    public string Titre { get; set; }
    public string Description { get; set; }
    public string DateCloture { get; set; }
    public List<string> ListeQuestions { get; set; }
}

And the view:

@model ProjetESGIForm.Models.Form

@{
    ViewBag.Title = "Detail";
}

<h2>Detail</h2>

<table>
    <tr>
        <td>
            @Html.LabelFor(m => m.Titre)
        </td>
        <td>
            @Html.LabelFor(m => m.Description)
        </td>
        <td>
            @Html.LabelFor(m => m.DateCloture)
        </td>
    </tr>
</table>

When I debug the project all the views work except the detail one ... I have the titre description and datecloture but no datas ...

If you have any idea, you're welcome !

Thanks.


Solution

  • Change View to :

    <table>
    <tr>
        <td>
            @Html.LabelFor(m => m.Titre)
            @Html.DisplayFor(m => m.Titre)
        </td>
        <td>
            @Html.LabelFor(m => m.Description)
            @Html.DisplayFor(m => m.Description)
        </td>
        <td>
            @Html.LabelFor(m => m.DateCloture)
            @Html.DisplayFor(m => m.DateCloture)
        </td>
    </tr>