0

这是我的模型:

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;

}

在此视图中,我输入资源的标题 (TitreR)、描述 (Desc),然后从标签列表中选择要与此资源链接的标签以及输入标签:

@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>

(我没有为它添加 javascript)

最后,控制器是:

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();

    }

我不断得到:对象引用未设置为对象的实例就行了: @Html.ValidationSummary(true)在我看来。你能告诉我如何解决它吗?

谢谢

4

1 回答 1

2

每次我得到那个错误都是因为没有初始化一些东西。查看您的代码,我看到的唯一可能需要它的地方是您设置结构时。您可以尝试在该类上放置一个构造函数来初始化字段,看看是否有帮助。就像是

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

您可能需要添加 { get; 放; } 到结构下的资源和列表。希望这会有所帮助。

于 2013-07-26T15:10:12.623 回答