NullReferenceException dans le fichier cshtml Edit
Bonjour,
Je suis entrain de suivre le tutoriel: http://www.asp.net/mvc/tutorials/get...vc-application
et la je me suis trouvé face à un problème dont je n'ai pas su résoudre, pourtant ça me parait que j'ai écris exactement comme le tutoriel.
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
| @model association.Models.reunion
@{
ViewBag.Title = "Edit";
}
<h2>Edit</h2>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<fieldset>
<legend>reunion</legend>
@Html.HiddenFor(model => model.reunionID)
<div class="editor-label">
@Html.LabelFor(model => model.sujet)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.sujet)
@Html.ValidationMessageFor(model => model.sujet)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.pv)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.pv)
@Html.ValidationMessageFor(model => model.pv)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.datereunion)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.datereunion)
@Html.ValidationMessageFor(model => model.datereunion)
</div>
<div class="editor-field">
<table>
<tr>
@{
int cnt = 0;
List<association.ViewModels.CreateAssignedMemberData> membres = ViewBag.membres;
foreach (var membre in membres) {
if (cnt++ % 3 == 0) {
@: </tr><tr>
}
@: <td>
<input type="checkbox"
name="selectedCourses"
value="@membre.PersonneID"
@(Html.Raw(membre.Assigned ? "checked=\"checked\"" : "")) />
@membre.PersonneID @: @membre.Nom
@:</td>
}
@: </tr>
}
</table>
</div>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div> |
L'erreur NullReferenceException est signalé au niveau du "foreach (var membre in membres)".
Nb: la classe "membre" hérite de la classe "personne"
La classe CreateAssignedMemberData se trouve sous un dossier appelé "ViewModels" et contient ce code:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
namespace association.ViewModels
{
public class CreateAssignedMemberData
{
public int PersonneID { get; set; }
public string Nom { get; set; }
public string Prenom { get; set; }
public bool Assigned { get; set; }
}
} |
classe reunion:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
namespace association.Models
{
public class reunion
{
public int reunionID { set; get; }
public string sujet { set; get; }
public string pv { set; get; }
[DisplayFormat(DataFormatString = "{0:d}")]
public DateTime datereunion { set; get; }
public virtual ICollection<membre> membres { get; set; }
}
} |
Classe membre:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using association.Models;
using System.ComponentModel.DataAnnotations;
namespace association.Models
{
public class membre : personne
{
public DateTime dateinscription { get; set; }
public virtual profil profil { get; set; }
public virtual ICollection<reunion> reunions { get; set; }
}
} |
Pouvez vous m'aidez? merci