salut

j'ai creer un formulaires (avec la validation) pour ajour des formations dans une table


Contrôleur : FormationController.cs


Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public ActionResult Create([Bind(Exclude = "Id")] Contact contactToCreate)
        {
 
            if (formationToCreate.intitule_formation.Trim().Length == 0)
                ModelState.AddModelError("intitule_formation", "intitule_formation iss required");
            if (!ModelState.IsValid)
                return View();
 
            try{
                gfc_Entities _entities = new gfc_Entities();
                _entities.AddToformation(formationToCreate);
                _entities.SaveChanges();
                return RedirectToAction("Index");
                }
            catch{
                return View();
                }
 
        }

Views : Create.ascx




Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<gfc.Models.formation>" %>
 
 
        <%: Html.ValidationSummary() %>
        <% using (Html.BeginForm()) {%>
        <fieldset>
            <legend>Fields</legend>
 
            <div class="editor-label">
                <%: Html.LabelFor(model => model.id_formation) %>
            </div>
            <div class="editor-field">
                <%: Html.TextBoxFor(model => model.id_formation) %>
                <%= Html.ValidationMessage("id_formation", "*")%>
                <!--%: Html.ValidationMessageFor(model => model.id_formation) %-->
            </div>
 
            <div class="editor-label">
                <%: Html.LabelFor(model => model.intitule_formation) %>
            </div>
            <div class="editor-field">
                <%: Html.TextBoxFor(model => model.intitule_formation) %>
                <%= Html.ValidationMessage("intitule_formation", "*")%>
                <!--%: Html.ValidationMessageFor(model => model.intitule_formation) %-->
            </div>
            <p>
                <input type="submit" value="Create" />
            </p>
        </fieldset>
 
    <% } %>
 
    <div>
        <%: Html.ActionLink("Back to List", "ListFormation") %>
    </div>

mais en cours de test si je laisse le champ intitule_formation je obtenue l'erreur :

erreur :

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 25: {
Line 26: if (formationToCreate.intitule_formation.Trim().Length == 0)
Stack Trace:
[NullReferenceException: Object reference not set to an instance of an object.]
gfc.Controllers.FormationController.Create(formation formationToCreate) in A:\0. New\Code\mvc\gfc\gfc\Controllers\FormationController.cs:27
lambda_method(Closure , ControllerBase , Object[] ) +162