0

我仍在学习 ASP.NET/MVC 并遵循 Matt Blagden(You Tube)关于构建博客的教程。

在制作帖子控制器后,如果它是一个新帖子,我在处理将标签添加到帖子的代码片段中出现错误。我的想法是添加新的实体数据模型时应该生成 AddToPosts 方法。

错误是:

blog.Models.BlogModel 不包含“AddToPosts”的定义,也没有扩展方法“AddToPosts”。可以找到接受“blog.Models.BlogModel”类型的第一个参数的 PostsController.cs(您是否缺少 using 指令或程序集引用?)

给我错误的代码部分是:

            if (!id.HasValue)
        {
            model.AddToPosts(post);

        }

这是整个后控制器

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using blog.Models;
using System.Data.EntityModel;
using System.Text;

namespace blog.Controllers
{
public class PostsController : Controller
{
    // Access Model
    private BlogModel model = new BlogModel();

    public ActionResult Index()
    {
        return View();
    }

    // A way to input sample data into database
    [ValidateInput(false)]
    public ActionResult Update(int? id, string title, string body, etc.....)
    {
        //IF not an admin redirect back to index 
        if (!IsAdmin)
        {
            return RedirectToAction("Index");
        }

        // Get Post
        Post post = GetPost(id);
        // Populate simple properties

        post.Title = title;
        post.Body = body;
        post.DateTime = dateTime;
        post.Tags.Clear();  // first clear tag list

        //ensure input sequence of tag names is not null
        tags = tags ?? string.Empty;

        //Split the sequence into a list of tag names
        string[] tagNames = tags.Split(new char[] { ' ' }, etc...

        //Get or create each tag that was named
        foreach (string tagName in tagNames)
        {
            //Add the tag
            post.Tags.Add(GetTag(tagName));

        }

        if (!id.HasValue)
        {
            model.AddToPosts(post);

        }

        model.SaveChanges();
        return RedirectToAction("Details", new { id = post.ID });
    }


    // PROMPTS USER TO INPUT DATA FOR DATABASE
    public ActionResult Edit(int? id)
    {
        Post post = GetPost(id);

        //ACCUMULATES LIST OF CURRENT TAGNAMES
        StringBuilder tagList = new StringBuilder();

        foreach (Tag tag in post.Tags)
        {
            //Append each tagname
            tagList.AppendFormat("{0} ", tag.Name);
        }
        //Gives the tagList to the view
        ViewBag.Tags = tagList.ToString();

        return View(post);

    }


    private Tag GetTag(string tagName)
    {   // if tag is set then Get the Tag, if not create a new one (Just like GetPost)
        return model.Tags.Where(x => x.Name == tagName).FirstOrDefault() ?? etc....

    }



    private Post GetPost(int? id)
    {
        // IF id is set then Get the Post, if not make a new one..
        return id.HasValue ? model.Posts.Where(x => x.ID == id).First() : etc.....


    // TODO: don't just return true

    public bool IsAdmin
    /* READS SESSION STATE */
    {
        get { return true; /*{ return Session["IsAdmin"] != null && etc...
    }
}

}

4

1 回答 1

0

如果“AddToPosts”来自静态类,请尝试在 Add post 类所在的名称空间中添加 using 语句。

于 2017-05-16T11:34:02.573 回答