Asp.Net MVC : Mon Formulaire ne renvoie pas mes données.
Bonjour,
J'ai créé un Controller et ses vues fortement typé (Create, Details, Index) à partir d'objet créé par Entity Framework comme je l'ai fais des dizaines de fois.
Seulement cette fois, mon formulaire Create ne me renvois pas mon objet, et j'obtiens un null décevant. J'ai vérifié avec un autre Controller et une autre vue Create de mon projet qui marche parfaitement et je ne vois pas la moindre différence.
Voici le Controller :
Code:
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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
|
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SiteFoncierOperation.Models.FoncierOperationDB;
namespace SiteFoncierOperation.Controllers
{
public class CritereController : BaseController
{
//
// GET: /Critere/
public ActionResult Index()
{
var critere = dbFO.Critere.Include("GroupeTravaux");
return View(critere.ToList());
}
//
// GET: /Critere/Details/5
public ActionResult Details(int id = 0)
{
Critere critere = dbFO.Critere.Single(c => c.idCritere == id);
if (critere == null)
{
return HttpNotFound();
}
return View(critere);
}
//
// GET: /Critere/Create
public ActionResult Create()
{
ViewBag.idGroupeTravaux = new SelectList(dbFO.GroupeTravaux, "idGroupeTravaux", "libelleGroupeTravaux");
return View();
}
//
// POST: /Critere/Create
[HttpPost]
public ActionResult Create(Critere critere)
{
if (ModelState.IsValid)
{
dbFO.Critere.AddObject(critere);
dbFO.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.idGroupeTravaux = new SelectList(dbFO.GroupeTravaux, "idGroupeTravaux", "libelleGroupeTravaux", critere.idGroupeTravaux);
return View(critere);
}
[...] |
La vue :
Code:
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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
|
@model SiteFoncierOperation.Models.FoncierOperationDB.Critere
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>Critere</legend>
<div class="editor-label">
@Html.LabelFor(model => model.estVisible)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.estVisible)
@Html.ValidationMessageFor(model => model.estVisible)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.critere)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.critere)
@Html.ValidationMessageFor(model => model.critere)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.commentaire)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.commentaire)
@Html.ValidationMessageFor(model => model.commentaire)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.idGroupeTravaux, "GroupeTravaux")
</div>
<div class="editor-field">
@Html.DropDownList("idGroupeTravaux", String.Empty)
@Html.ValidationMessageFor(model => model.idGroupeTravaux)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
} |
L'objet Critere :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
namespace SiteFoncierOperation.Models.FoncierOperationDB
{
[MetadataType(typeof(CritereMetaData))]
public partial class Critere
{
}
public class CritereMetaData
{
public int idCritere { get; set; }
public bool estVisible { get; set; }
public string critere { get; set; }
public string commentaire { get; set; }
public int idGroupeTravaux { get; set; }
}
} |
Forcément je me prend un NullPointerException quand j'essaye de récupérer les Groupes de travaux au retour de mon formulaire Create, à la ligne 59 de mon Controller. Vu que la variable "critere" est à null.
Donc si vous aviez des pistes à me proposer, j'en serai ravi!
Merci ^^.