Exception de type : InvalideOperationException
Salut je viens à vous une fois de plus car je bute face à une incompréhension Voilá lorsque je tente une opération d'insertion dans ma Table Note de ma BD,j'ai une InvalideOperationException qui retourne message d'erreur
Citation:
There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'Coefvalue'.
Cependant je ne comprend pas pourquoi car je suppose que mes instructions sont correctées bref voici un extrait de mon code :
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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
| // ma vue
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<MvcApplication3.Models.Note>" %>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2> Enregistrement des Notes </h2>
<% using (Html.BeginForm()) {%>
<%: Html.ValidationSummary(true) %>
<fieldset>
<legend>Fields</legend>
<div class="editor-label" style="display: none">
<%: Html.LabelFor(model => model.NoteId) %>
</div>
<div class="editor-field" style="display: none">
<%: Html.TextBoxFor(model => model.NoteId) %>
<%: Html.ValidationMessageFor(model => model.NoteId) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.Notevalue) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.Notevalue) %>
<%: Html.ValidationMessageFor(model => model.Notevalue) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.Coef) %>
</div>
<div class="editor-field">
<%: Html.DropDownList("Coefvalue") %>
<%: Html.ValidationMessageFor(model => model.Coef) %>
</div>
<div class="editor-label" style="display: none">
<%: Html.LabelFor(model => model.Notecoef) %>
</div>
<div class="editor-field" style="display: none">
<%: Html.TextBoxFor(model => model.Notecoef) %>
<%: Html.ValidationMessageFor(model => model.Notecoef) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.Codematiere) %>
</div>
<div class="editor-field">
<%: Html.DropDownList("Codematieres") %>
<%: Html.ValidationMessageFor(model => model.Codematiere) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.Mention) %>
</div>
<div class="editor-field">
<%: Html.DropDownList("Mentions") %>
<%: Html.ValidationMessageFor(model => model.Mention) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.MatriculeEtudiant) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.MatriculeEtudiant) %>
<%: Html.ValidationMessageFor(model => model.MatriculeEtudiant) %>
</div>
<div class="editor-label" style="display: none">
<%: Html.LabelFor(model => model.UserAccountId) %>
</div>
<div class="editor-field" style="display: none">
<%: Html.TextBoxFor(model => model.UserAccountId) %>
<%: Html.ValidationMessageFor(model => model.UserAccountId) %>
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
<% } %>
<div>
<%: Html.ActionLink("Back to List", "Index") %>
</div>
</asp:Content> |
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 63 64 65 66 67 68 69
| // mon controller
public ActionResult AddNote()
{
List<SelectListItem> coefs = new List<SelectListItem>();
var coef = from co in _db.Coefs
select co;
foreach (var item in coef)
{
coefs.Add(new SelectListItem {Text = ""+item.Coefvalue ,Value=item.Coefvalue.ToString() });
}
ViewData["Coefvalue"] = coefs;
List<SelectListItem> codematieres = new List<SelectListItem>();
var code = from mat in _db.Matieres
select mat;
foreach (var item in code)
{
codematieres.Add(new SelectListItem {Text = item.Codematiere , Value= item.Codematiere});
}
ViewData["Codematieres"] = codematieres;
List<SelectListItem> mention = new List<SelectListItem>();
var ment = from men in _db.Mentions
select men;
foreach (var item in ment)
{
mention.Add(new SelectListItem { Text=item.Mentionname ,Value=item.Mentionname});
}
ViewData["Mentions"] = mention;
return View();
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult AddNote(FormCollection form)
{
var NoteToAdd = new Note();
// Désérialisation (Inclus une simple vide !)
TryUpdateModel(NoteToAdd, new string[] { "Notevalue", "Coef", "NoteCoef", "Codematiere","Mention","MatriculeEtudiant","UserAccountId" }, form.ToValueProvider());
// Validation
if (NoteToAdd.Notevalue == null)
ModelState.AddModelError("Notevalue", "Note is required!");
if (NoteToAdd.Coef == null)
ModelState.AddModelError("Coef", "NoteCoef is required!");
if (String.IsNullOrEmpty(NoteToAdd.Codematiere))
ModelState.AddModelError("codematiere", "codematiere Etudiant is required!");
if (String.IsNullOrEmpty(NoteToAdd.MatriculeEtudiant))
ModelState.AddModelError("Matricule", "Matricule Etudiant is required!");
// Si valide, sauvegarde le film dans la base de données
if (ModelState.IsValid)
{
NoteToAdd.Notecoef = NoteToAdd.Notevalue * NoteToAdd.Coef;
_db.AddToNotes(NoteToAdd);
_db.SaveChanges();
return RedirectToAction("ListeNote");
}
// Sinon, réaffiche la page
return View(NoteToAdd);
} |
Esperant quel l'un d'entre nous puisse voir ce donc je ne vois pas ou comprendre ce donc je ne comprend pas, toute aide, assistance ou conseil serais la bienvenue.
a++ :)