0

this is my models:

public class Ressource
{
    [Key]
    public int RessourceId { get; set; }
    public string TitreR { get; set; }
    public string Link { get; set; }
    public string Type { get; set; }
    public string Desc { get; set; }
    public int Position { get; set; }
    public int Rating { get; set; }
    public string Tags { get; set; }
    public int SectionId { get; set; }
    public int UserId { get; set; }

}
public class Section
{
    [Key]
    public int SectionId { get; set; }
    public string Titre { get; set; }
    public String Tags { get; set; }

    public virtual ICollection<Ressource> Ressources { get; set; }

}
public class structure
{

        public Ressource ress;
        public List<string> liste;

}

In this view, I enter the resource's title (TitreR), a description (Desc) and I choose from a list of tags which ones I want to link with this resource as well as entering a tag:

@model Mocodis.Models.structure

@{
    ViewBag.Title = "AjouterRessource";
}

<h2>AjouterRessource</h2>

@using (Html.BeginForm("AjouterRessource", "Home", FormMethod.Post))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)

<fieldset>
    <input type="hidden" name="SectionId" value="@Model.ress.SectionId"  />
    <legend>Ressource</legend>

    <div class="editor-label">
        @Html.LabelFor(model => model.ress.TitreR)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.ress.TitreR)
        @Html.ValidationMessageFor(model => model.ress.TitreR)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.ress.Desc)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.ress.Desc)
        @Html.ValidationMessageFor(model => model.ress.Desc)
    </div>
</fieldset>
}
    <form id="orderProductForm219" name="orderProductForm219" method="post" >
<b>Choisir les mots clés:</b> 
<br /><br />


@foreach (string s in @Model.liste)
{
    <input type="checkbox" name="tags[]" value="@s"> @s<br />
}
<input type="text" name="tags" id="tags" value="" /> 


    <p>
        <input type="submit" value="Create" />
        <input type="hidden" name="tag" value="tags" />
        <input type="hidden" name="res" value="@Model.ress" />
    </p></form>

(I didnt put the javascript for it)

Finally the controllers are:

public ActionResult AjouterRessource(int SectionId)
    {

        Ressource res = new Ressource();
        res.SectionId = SectionId;
        Section sec = _db.Sections.Where(r => r.SectionId == SectionId).FirstOrDefault();
        char[] delimiterChars = { ' ', ',', '.', ':', '\t' };
        List<string> l = new List<string>();
        l = sec.Tags.Split(delimiterChars).ToList();

        structure s = new structure();
        s.ress = res;
        s.liste = l;

        return View(s);

    }

    public string Check ( string list, string s)
    {
        char[] delimiterChars = { ' ', ',', '.', ':', '\t' };
        List<string> l = new List<string>();
        List<string> liste = new List<string>();
        l = s.Split(delimiterChars).ToList();
        liste = list.Split(delimiterChars).ToList();
        foreach (string item in l)
        {
            if (!liste.Contains(item))
                liste.Add(item);
        }
        return  (string.Join(" ", liste.ToArray()));
    }

    [Authorize]
    [HttpPost]
    [InitializeSimpleMembership]
    public ActionResult AjouterRessource(Ressource res, int SectionId, string tag)
    {
        if (ModelState.IsValid)
        {

            res.SectionId = SectionId;
            var model = _db.Sections.Where(c => c.SectionId == SectionId).FirstOrDefault();
            res.Tags = tag;
            model.Tags = Check(model.Tags, tag);
            _db.Entry(model).State = EntityState.Modified;
            _db.Entry(res).State = EntityState.Added;
            _db.SaveChanges();
            return RedirectToAction("Section", new { SectionId = SectionId });
        }
        return View();

    }

I keep getting: Object reference not set to an instance of an object on the line: @Html.ValidationSummary(true) in my view. Can you tell me how to fix it please?

Thank you

0

1 Answer 1

2

Every time I have gotten that error it has been from not initializing something. Looking through your code the only spot I am seeing that might need it is when you are setting structure. You might try putting a constructor on that class to initialize the fields and see if that helps. something like

public structure()
{
ress = new Resource();
liste = new List<string>();
}    

You probably need to add the { get; set; } to the resource and list under structure as well. Hopefully this helps.

Sign up to request clarification or add additional context in comments.

1 Comment

Yeah I figured it out, that was the problem! I keep forgetting to initialize! Thank you :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.