[mvc] Comment insérer un objet typé contenant une propriété typée
Salut,
Mon code retourne une Exception de Référence Nulle, je comprends pourquoi mais ne sait pas corriger l'erreur.
Tout d'abord, voici à quoi ressemble l'entité :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| namespace NS.Entities
{
[Serializable]
public class Stuff
{
public double Price { get; set; }
public StuffFeatures Features { get; set; }
}
[Serializable]
public class StuffFeatures
{
public bool Feature1 { get; set; }
public bool Feature2 { get; set; }
public bool Feature3 { get; set; }
public bool Feature4 { get; set; }
public bool Feature5 { get; set; }
}
} |
Vous pouvez voir que la propriété Features a un type StuffFeatures. C'est ce qui pose problème. J'utilise donc un contrôleur et sa vue pour insérer un objet Stuff en base :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| <%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<NS.Entities.Stuff>" %>
<%@ Import Namespace="NS.Common.Enums" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
<form id="create" method="post" action="Create">
<p><label for="price">price</label>
<input id="price" type="text" name="price" />
</p>
<fieldset>
<legend>features</legend>
<ul>
<% foreach (string n in Enum.GetNames(typeof(Features))) { %>
<li>
<label for="<%=string.Format("Features.{0}",n) %>"><%=n %></label>
<input id="<%=string.Format("Features.{0}",n) %>" name="<%=string.Format("Features.{0}",n) %>" value="True" type="checkbox" />
</li>
<%} %>
</ul>
</fieldset>
<button id="submit" type="submit" value="save" name="submit">save</button>
</form>
</asp:Content> |
Code:
1 2 3 4 5 6 7
| [AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(Stuff stuffToCreate)
{
StuffsService stuffsService = new StuffsService(ModelState);
stuffsService.Insert(stuffToCreate);
return View();
} |
Maintenant le problème survient au moment de l'insertion :
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
| namespace NS.Repositories
{
using System;
using System.Collections.Generic;
using System.Linq;
using NS.Entities;
using NS.Repositories.Basic;
public class StuffRepository
{
public public class StuffRepository() : base(new NSDataContext()) { }
public override Stuff Insert(Stuff item)
{
STUFFS stuff = new STUFFS();
stuff.PRICE = item.Price;
_db.STUFFS.InsertOnSubmit(stuff);
STUFF_FEATURES stuffFeatures = new STUFF_FEATURES();
stuffFeatures.FEATURE_1 = item.Features.Feature1; //<-- ici !
stuffFeatures.FEATURE_2 = item.Features.Feature2;
stuffFeatures.FEATURE_3 = item.Features.Feature3;
stuffFeatures.FEATURE_4 = item.Features.Feature4;
stuffFeatures.FEATURE_5 = item.Features.Feature5;
_db.STUFF_FEATURES.InsertOnSubmit(stuffFeatures);
_db.SubmitChanges();
return item;
}
}
} |
Donc item.Features.Feature1 provenant de la vue n'est pas interprété comme étant l'équivalent de Stuff.Features.Feature1.
Savez-vous s'il est possible d'agir ainsi? Si oui, comment?
Merci par avance.