Etienne Poulin
Etienne Poulin

Reputation: 23

Model returns null when the site first loads

So, I recently found quite an issue with my site: when it first loads, a section of the website is missing. After some tests, I found that this line was sometimes false: @if (Model != null && Model.Any()). After a test using a single Modal == null, I found that yes, the issue is that it's sometimes null. Also, I found that the best way for me to reproduce the issue (no error messages) is to restart visual studio. CTRL + F5 does not make it be null. Any ideas why is that ? Here's the Model and the part of 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())
//...

Note that I'm using asp.net Core MVC with razor.

Edit:

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

Upvotes: 0

Views: 359

Answers (1)

Etienne Poulin
Etienne Poulin

Reputation: 23

Found it... It was checking for null and if it was, was adding it to cache but still returning the non-updated variable. Simply had to update it before returning...

Added:

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

before returning.

Upvotes: 1

Related Questions