数据模型内容未显示在新控制器/视图MVC5上

时间:2018-04-18 12:16:21

标签: c# asp.net asp.net-mvc razor

我正在尝试在帖子中显示标题。但它不会列在与Post控制器一起使用的新索引中。

这是索引;

    @using Shegzstuffweb.Models;
    @model IEnumerable<Post>
    @{
      ViewBag.Title = "Index";
    }
    @foreach (Post post in Model)
    {
      @post.Title <br />
    }

后控制器是;

    public class PostController : Controller
{
    private BlogModel model = new BlogModel();



    public ActionResult Index()
    {
        IEnumerable<Post> posts =
            from post in model.Posts
            where post.DateTime < DateTime.Now
            orderby post.DateTime descending
            select post;
        ViewBag.IsAdmin = IsAdmin;


        return View(posts);
    }




    [ValidateInput(false)]

    public ActionResult Update(int? id, string title, string body, DateTime dateTime, string tags)
    {
        if (!IsAdmin)
        {
            return RedirectToAction("index");
        }

        Post post = GetPost(id);
        post.Title = title;
        post.Body = body;
        post.DateTime = dateTime;
        post.Tags.Clear();

        tags = tags ?? String.Empty;
        string[] tagNames = tags.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

        foreach (string tageName in tagNames)
        {
            post.Tags.Add(GetTag(tageName));

        }
        if (!id.HasValue)
        {
            model.Posts.Add(post);
        }
        model.SaveChanges();
        return RedirectToAction("Details", new { id = post.ID });


    }

    public ActionResult Edit(int? id)
    {
        Post post = GetPost(id);
        StringBuilder tagList = new StringBuilder();
        foreach (Tag tag in post.Tags)
        {
            tagList.AppendFormat("{0}", tag.Name);
        }
        ViewBag.Tags = tagList.ToString();
        return View(post);
    }

    private Tag GetTag(string tagName)
    {
        return model.Tags.Where(x => x.Name == tagName).FirstOrDefault() ?? new Tag() { Name = tagName };
    }

    private Post GetPost(int? id)
    {
        return id.HasValue ? model.Posts.Where(x => x.ID == id).First() : new Post() { ID = -1 };
    }

    // TODO
    public bool IsAdmin { get { return true; /*Session["IsAdmin"] != null && (bool)Session["IsAdmin"]; */   } }
}

}

我只在共享布局中看到页眉和页脚。

像这样;

the screenshot of when index runs on IE

我研究过并被告知我必须添加一条路线。所以我在App_start / RouteConfig.cs

中添加了它 它成了。

     public class RouteConfig
       {
    public static void RegisterRoutes(RouteCollection routes)
          {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
          name: "Index",
          url: "{controller}/{action}/{id}",
          defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
      );

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Post", action = "Index", id = UrlParameter.Optional }
        );
    }
} 

请问我做错了什么?

如何获取数据以显示标题和后续修改?

你可能已经注意到了,我正在使用两种观点。我应该在Home控制器上构建项目吗?

0 个答案:

没有答案