Bonjour tout le monde,

voici mon contexte :

Controller:
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
 
        // GET: PixelBlog/Edit/5
        public ActionResult Edit(int id)
        {
            List<SelectListItem> listSelectListItems = new List<SelectListItem>();
 
            foreach (Category cat in db.Categories)
            {
                SelectListItem selectList = new SelectListItem()
                {
                    Text = cat.Name,
                    Value = cat.Id.ToString()
                };
                listSelectListItems.Add(selectList);
            }
 
            string[] arr1 = new string[] { "NULL" };
 
            PostCategoryLink postCatViewModel = new PostCategoryLink()
            {
                GetCategories = listSelectListItems,
                GetCategoriesName = arr1,
                GetPosts = db.Posts.Find(id)
            };
            return View(postCatViewModel);             
        }
 
        // POST: PixelBlog/Edit/5
        [HttpPost]
        public ActionResult Edit(PostCategoryLink postCatViewModel)
        {
            try
            {
                // TODO: Add update logic here 
                string s_catName;
                if (postCatViewModel.GetCategoriesName == null)
                {
                    s_catName = "Null";
                }
                else
                {
                    System.Text.StringBuilder sb = new System.Text.StringBuilder();
                    sb.Append("You selected – " + string.Join(",", postCatViewModel.GetCategoriesName[0]));
                    s_catName = sb.ToString();
                }
 
                postCatViewModel.GetPosts.CategoryID = int.Parse(postCatViewModel.GetCategoriesName[0]);                
 
                var context = new PixelBlogContext();
 
                context.Entry(postCatViewModel.GetPosts).State = System.Data.Entity.EntityState.Modified;
                context.SaveChanges();
                return RedirectToAction("Index");
            }
            catch (Exception e)            
            {
                return View();
            }
        }
Models:

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
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PixelBlog.Models;
 
namespace PixelBlog.ViewModels
{
    public class PostCategoryLink
    {
        public Post GetPosts { get; set; }
        public string[] GetCategoriesName { get; set; }
        public IEnumerable<SelectListItem> GetCategories { get; set; }
    }
}
 
 
 
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Web;
 
namespace PixelBlog.Models
{
    #region Post
    public class Post
    {
        /// <summary>
        /// By convention, ID is a primary key
        /// </summary>
        /// 
        public virtual int Id { get; set; }
 
        ///<summary>
        /// Title Field
        /// </summary>
        /// 
        public virtual string Title { get; set; }
 
        ///<summary>
        /// ShortDescription Field
        /// </summary>
        /// 
        public virtual string ShortDescription { get; set; }
 
        ///<summary>
        /// Description Field
        /// </summary>
        ///
        public virtual string Description { get; set; }
 
        ///<summary>
        ///Meta Field
        /// </summary>
        /// 
        public virtual string Meta { get; set; }
 
        /// <summary>
        /// UrlSlug Field
        /// </summary>
        /// 
        public virtual string UrlSlug { get; set; }
 
        /// <summary>
        /// Rate Field
        /// </summary>
        /// 
        public virtual int Rate { get; set; }
 
        /// <summary>
        /// Published Field
        /// </summary>
        /// 
        public virtual bool Published { get; set; }
 
        ///<summary>
        /// PostedOn Field
        /// </summary>
        ///
        public virtual DateTime PostedOn { get; set; }
 
        ///<summary>
        /// CategoryID Field
        /// </summary>
        /// 
        [Required]
        public int CategoryID { get; set; }
 
        ///<summary>
        /// Category FK Field
        /// </summary>
        /// 
        [ForeignKey("CategoryID")]
        public virtual Category Category { get; set; }
 
        ///<summary>
        /// TagID Field
        /// </summary>
        /// 
        [Required]
        public int TagID { get; set; }
 
        /// <summary>
        /// Tags FK Field
        /// </summary>
        /// 
        [ForeignKey("TagID")]
        public virtual Tag Tag { get; set; }
    }
    #endregion
}
Et ma vue :

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
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
89
90
91
92
93
94
95
96
97
 
@using PixelBlog.Models
@model PixelBlog.ViewModels.PostCategoryLink
 
@{
    Layout = "~/Views/Shared/_Layout.cshtml";
    ViewBag.Title = "Details post";
}
 
@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
 
    <div class="form-horizontal">
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        @Html.HiddenFor(model => model.GetPosts.Id)
 
        <div class="form-group">
            @Html.LabelFor(model => model.GetPosts.Title, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.GetPosts.Title, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.GetPosts.Title, "", new { @class = "text-danger" })
            </div>
        </div>
 
        <div class="form-group">
            @Html.LabelFor(model => model.GetPosts.ShortDescription, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.GetPosts.ShortDescription, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.GetPosts.ShortDescription, "", new { @class = "text-danger" })
            </div>
        </div>
 
        <div class="form-group">
            @Html.LabelFor(model => model.GetPosts.Description, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.GetPosts.Description, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.GetPosts.Description, "", new { @class = "text-danger" })
            </div>
        </div>
 
        <div class="form-group">
            @Html.LabelFor(model => model.GetPosts.Meta, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.GetPosts.Meta, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.GetPosts.Meta, "", new { @class = "text-danger" })
            </div>
        </div>
 
        <div class="form-group">
            @Html.LabelFor(model => model.GetPosts.Rate, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.GetPosts.Rate, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.GetPosts.Rate, "", new { @class = "text-danger" })
            </div>
        </div>
 
        <div class="form-group">
            @Html.LabelFor(model => model.GetPosts.UrlSlug, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.GetPosts.UrlSlug, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.GetPosts.UrlSlug, "", new { @class = "text-danger" })
            </div>
        </div>
 
        <div class="form-group">
            @Html.LabelFor(model => model.GetPosts.Published, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                <div class="checkbox">
                    @Html.EditorFor(model => model.GetPosts.Published)
                    @Html.ValidationMessageFor(model => model.GetPosts.Published, "", new { @class = "text-danger" })
                </div>
            </div>
        </div>
 
        <div class="form-group">
            @Html.LabelFor(model => model.GetPosts.PostedOn, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.GetPosts.PostedOn, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.GetPosts.PostedOn, "", new { @class = "text-danger" })
            </div>
        </div>
 
        @Html.DropDownListFor(m => m.GetCategoriesName, Model.GetCategories)
 
        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Save" class="btn btn-default" />
            </div>
        </div>
    </div>
}
 
<div>
    @Html.ActionLink("Back to List", "Index")
</div>
J'ai une erreur : La référence d'objet n'est pas définie à une instance d'un objet
sur la ligne @Html.DropDownListFor(m => m.GetCategoriesName, Model.GetCategories)

Je pense que l'erreur vient du faite que dans ma classe Post, les champs :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
 
[ForeignKey("CategoryID")]
        public virtual Category Category { get; set; }
 
[ForeignKey("TagID")]
        public virtual Tag Tag { get; set; }
sont à NULL lorsque je passe cela ici :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
 
context.Entry(postCatViewModel.GetPosts).State = System.Data.Entity.EntityState.Modified;
                context.SaveChanges();
La classe PostCategoryLink me sert à faire la liaison entre les classe Post et Category pour pouvoir afficher dans la dropdown la liste des mes catégories.

Je pense que je me suis un peu perdu dans mon code (je débute le MVC Razor), il y a-t-il plus simple ?

Je vous remercie pour votre aide et bon week-end !