网站首次加载时,模型返回null

时间:2018-07-18 18:36:06

标签: asp.net asp.net-mvc razor model-view-controller asp.net-core

因此,我最近发现我的网站存在很多问题:首次加载时,网站的一部分丢失了。经过一些测试,我发现这一行有时是错误的:@if (Model != null && Model.Any())。在使用单个Modal == null进行测试后,我发现是的,问题在于有时是null。另外,我发现重现此问题的最佳方法(无错误消息)是重新启动Visual Studio。 CTRL + F5不会使其为空。有什么想法吗? 这是Model和CSHTML的一部分:

public class BlogModel
{
    public int Id { get; set; }
    public bool AfficheAuteur { get; set; }
    public string Alias { get; set; }
    public string Sujet { get; set; }
    public string Auteur { get; set; }
    public string Photo { get; set; }

    public int? Ordre { get; set; }
    public PostModel Post { get; set; }
}

public class PostModel
{
    public int Id { get; set; }
    public string Alias { get; set; }
    public string Nom { get; set; }
}


//.cshtml:
@model IList<Project.Models.Shared.BlogModel>
//...
@if (Model != null && Model.Any())
//...

请注意,我正在使用带有剃须刀的asp.net Core MVC。

编辑:

public static IList<BlogModel> GetBlogs()
    {
        var _lock = new object();

        var strKey = string.Format("Home-Blogs-{0}", Site.Id);

        var blogs = (IList<BlogModel>)CacheManager.Get(strKey);

        if (blogs == null)
        {
            lock (_lock)
            {
                blogs = (IList<BlogModel>)CacheManager.Get(strKey);

                if (blogs == null)
                {
                    using (var context = new DB())
                    {
                        context.Configuration.LazyLoadingEnabled = false;

                        var nIdSite = Site.Id;

                        var bl = (from b in context.Blog
                                  where b.Actif &&
                                        (b.IdsSite.Contains("," + nIdSite + ",")) &&
                                        b.Posts.Any(y => y.Publier)
                                  orderby b.Ordre
                                  select new BlogModel()
                                    {
                                        Id = b.Id,
                                        AfficheAuteur = b.AfficherAuteur,
                                        Alias = b.Alias,
                                        Sujet = b.Sujet,
                                        Photo = b.Image,
                                        Auteur = b.User.Profile.FirstName + " " +  b.User.Profile.LastName,
                                        Ordre = b.Ordre,
                                        Post = (from p in context.BlogPost
                                                where p.Publier &&
                                                        p.IdBlog == b.Id &&
                                                        p.DateAffichage <= DateTime.Now
                                                orderby p.DateAffichage descending
                                                select new PostModel()
                                                    {
                                                        Id = p.Id,
                                                        Alias = p.Alias,
                                                        Nom = p.Nom
                                                    }).FirstOrDefault()
                                    }).ToList();


                        CacheManager.Insert(strKey, bl, null, 10800, Cache.NoSlidingExpiration, CacheItemPriority.High, null);

                        return blogs;
                    }
                }
            }
        }

        return blogs;
    }
public ActionResult Index(GridSettings settings, string strQuery)
    {
        var model = new IndexBlogViewModel(settings, blogService, strQuery);

        ViewBag.FilAriane.Add(new KeyValuePair<string, string>(Url.Action("Index", "Home"), "Accueil"));
        ViewBag.FilAriane.Add(new KeyValuePair<string, string>("", "Blogs"));

        return View(model);
    }
[HttpGet]
    public ActionResult Create()
    {
        var model = new BlogFormViewModel { Blog = new Entitie.Blog { IdSite = IdSite } };

        var lstUser = new List<User>();
        var cfUserProvider = new CFUserProvider();

        foreach (var mu in cfUserProvider.GetAllUsers().Cast<MembershipUser>())
        {
            var r = new CFRoleProvider();
            if (r.IsUserInRole(mu.UserName, "Bloggeur"))
            {
                var u = new User { Username = mu.UserName, Id = Convert.ToInt32(mu.ProviderUserKey) };
                lstUser.Add(u);
            }
        }

        model.User = lstUser.Select(x => new SelectListItem
        {
            Text = x.Username,
            Value = x.Id.ToString()
        });

        model.Sites = siteService.GetAll(x => x.IdEntreprise == IdEntreprise)
                                   .Select(x => new CheckBoxListItem
                                   {
                                       Id = x.Id,
                                       Display = x.Nom,
                                       IsChecked = false
                                   }).ToList();

        ViewBag.FilAriane.Add(new KeyValuePair<string, string>(Url.Action("Index", "Home"), "Accueil"));
        ViewBag.FilAriane.Add(new KeyValuePair<string, string>("", "Blog"));

        return View(model);
    } 

1 个答案:

答案 0 :(得分:1)

找到它...它正在检查null,如果是,则将其添加到缓存中,但仍返回未更新的变量。只需在返回之前更新它即可...

已添加:

blogs = (IList<BlogModel>)CacheManager.Get(strKey);

返回之前。