0

I'm fighting with my ASP.NET MVC site, when reloading records from database.

In my scenario, I have a site to insert some data. Additionally, I have a graphic with some svg, where these data are shown also.

If I now change any of the data and then reopen the graphic, only the old values are shown, even on reload of the page.

The data is loaded into a viewBag model, within the action method that opens the site, but the method, that loads the data returns these old values.

If I load the data after restarting the site (debug stop/start) everything is as it should.

Here, the data should be loaded, for the viewBag model:

EditorViewModel model = new EditorViewModel
        {
            Project = projectModel,
            ProjectSymbols = SymbolDataProvider.GetDxProjectSymbols(projectModel.Id),
            TemplateSymbols = SymbolDataProvider.GetDxTemplateSymbols(),
            UsedProjectSymbols = SymbolDataProvider.GetProjectSymbols(projectModel.Id),
            ProjectItems = SymbolDataProvider.GetProjectFunctionGroups(Id)
        };
return View(model);

This is where the data are loaded from database:

    public static IList<ProjectSymbols> GetProjectSymbols(int projectId)
    {
        string AKZ = "";
        string OKZ = "";

        IEnumerable<Symbol> symbols = db.Symbols.ToList();
        IList<SubFunction> subFunctions = db.SubFunctions.Where(x => x.Function.FunctionGroup.ProjectId == projectId).ToList();

        List<ProjectSymbols> projectSymbols = new List<ProjectSymbols>();

        foreach (SubFunction sbf in subFunctions)
        {
            if (!string.IsNullOrEmpty(sbf.SymbolPosition))
            {
                Symbol symbol = symbols.FirstOrDefault(x => x.Id == sbf.SymbolId);

                if (symbol != null)
                {
                    AKZ = "";
                    OKZ = "";
                    ProjectSymbols model = new ProjectSymbols
                    {
                        SVG = symbol.SvgString,
                        ProjectId = projectId,
                        SubFunctionId = sbf.Id,
                        Symbol = sbf.Symbol,
                        SymbolId = sbf.SymbolId,
                        SymbolPosition = sbf.SymbolPosition,
                        SymbolTypeId = sbf.SymbolTypeId,
                        SymbolVariant = sbf.SymbolVariant
                    };

                    if (string.IsNullOrEmpty(sbf.Function.FunctionSign))
                    { AKZ = sbf.Function.FunctionGroup.FunctionSign; }
                    else { AKZ = sbf.Function.FunctionSign; }

                    if (string.IsNullOrEmpty(sbf.Function.LocationSign))
                    { OKZ = sbf.Function.FunctionGroup.LocationSign; }
                    else { OKZ = sbf.Function.LocationSign; }

                    model.BMK = "=" + AKZ + "+" + OKZ;

                    projectSymbols.Add(model);
                }
            }
        }

        return projectSymbols;
    }

Actually the wrong data is loaded here:

AKZ = sbf.Function.FunctionGroup.FunctionSign;

As I could see in debug mode, if I load this FunctionSign, its value is different from the one in the table.

Thanks

7
  • How are you submitting your data back to the controller? Does it call a method in your controller attributed with the HTTPPost annotation? Does that method return a get instance and not just the view? You should make a call to refresh your data via a HTTP GET after you post to update it. Commented Dec 3, 2019 at 7:38
  • How are you loading your data from the database? Are you using Entity framework? Commented Dec 3, 2019 at 7:39
  • Oh yes, I'm using EF6. I will update the post with my Code... Commented Dec 3, 2019 at 7:42
  • Using EF, are you invoking the SaveChanges() method after you change the values? Commented Dec 3, 2019 at 7:43
  • Yes, the values are correct inside the table… As I wrote, when restarting the page, the values are shown correctly. Commented Dec 3, 2019 at 7:48

1 Answer 1

1

Your function GetProjectSymbols is a static so I assumed your db DbContext is also static. If the DbContext variable you use to save the data is not the same variable you use to load the data, then you need to reinitialize it before querying so it will get updated data from the database.

Put this line

db = new YourDbContext();

before

IEnumerable<Symbol> symbols = db.Symbols.ToList();
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, this was pretty simple explained. ...and it works perfect.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.