0

I'm working on an ASP.Net MVC project, and I've hit a (seemingly) very strange error. I have a model named Lens, here is the class structure for it

public class Lens
{
    public int LensID { get; set; }
    public string LensName { get; set; }
}

In one of my views, I have a list of lenses (acquired in controller). I'm getting an ArgumentOutOfRangeException in the following code snippet

@for (int i = 0; i < Lenses.Count; i++)
{
     <input type="text" data-LID="@Lenses[i].LensID" value="@Lenses[0].LensName" readonly id="lens @i" />
}

Lenses is the list I get from the controller. I really can't understand why this code is throwing an exception. I looked at the Locals view in my debugger and found that, at the time that the exception is thrown, the value of the argument i is 0, and there is a Lens object at index 0 of Lenses. Even stranger, when I use a foreach loop to pull from the exact same list, it has no issue retrieving the object. I've also tried

@Lenses[0].LensID

Instead, and I got the same error. Also, in case it's relevant. According to the Locals view, the Lenses list has a Count of 1. Here's the creation of Lenses

List<Lens> Lenses = (List<Lens>)ViewBag.Lenses;

And here's how it's being sent to the View from Controller

LensQueries lQuery = new LensQueries();
ViewBag.Lenses = lQuery.GetAll();

And here's the reference for LensQueries.GetAll()

public List<Lens> GetAll()
    {
        List<Lens> retList;
        using (var context = new SqlDbContext())
        {
            retList = context.Lenses.SqlQuery("select *from dbo.Lens").ToList<Lens>();
        }
        return retList;
    }
Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120
  • Not an answer to your question, but element ids cannot have spaces in them (`id="lens @i"`). – Heretic Monkey Jul 25 '18 at 20:45
  • What type is `Lenses`? – cwharris Jul 25 '18 at 20:45
  • no, `Lenses` is of type `TSomething`, what is `TSomething`? – cwharris Jul 25 '18 at 20:46
  • 1
    For the answer, why not just use a `foreach` loop, since it's working? If you must have an index, just add a variable and increment in the loop. – Heretic Monkey Jul 25 '18 at 20:46
  • 1
    Oh yes, my apologies, didn't think that comment through before submitting it, it's a List – Matthew Gower Jul 25 '18 at 20:47
  • 4
    And how are you sending the model to the view? Please show us that code. We need a [mcve]. I don't think `Lenses` contains what you think it contains. –  Jul 25 '18 at 20:48
  • 2
    As it stands, there is nothing wrong with this code (unless it is syntactical in nature). Is it possible that an older version of this code is executing, and not this code exactly? More information is needed, such as where specifically the exception is thrown, and potentially some `Controller` code. I could be wrong, but I don't see any problem. – cwharris Jul 25 '18 at 20:51
  • I added some relevant controller code, and I've cleaned/rebuilt the project countless times trying to fix this so I'm fairly certain it's running the right version of the code. The line where the exception being thrown is the statement in the code above. – Matthew Gower Jul 25 '18 at 20:53
  • @Heretic Monkey That is my plan, I'm more so asking for the sake of understanding why it's breaking, instead of just merely wanting to work around it. – Matthew Gower Jul 25 '18 at 20:54
  • 3
    You might consider using `return View(lQuery.GetAll());` and using an `@model List` rather than using `ViewBag` if you can. I've had nothing but problems with `ViewBag` and stay away from it like the plague... – Heretic Monkey Jul 25 '18 at 20:59
  • 1
    If I recall, I had seen quite similar issue before on .net core 1.0, and issue disappeared after upgrade to 2.1 – Bonelol Jul 25 '18 at 21:03
  • What's the full error message? – Jasen Jul 25 '18 at 21:03
  • 1
    Possible duplicate of [What is an IndexOutOfRangeException / ArgumentOutOfRangeException and how do I fix it?](https://stackoverflow.com/questions/20940979/what-is-an-indexoutofrangeexception-argumentoutofrangeexception-and-how-do-i-f) – Camilo Terevinto Jul 25 '18 at 21:44
  • @HereticMonkey On the opposite, I have never had issues with the ViewBag. That's a pretty bold comment when you have no facts to back it up. – Camilo Terevinto Jul 25 '18 at 21:45
  • 1
    @CamiloTerevinto Is there a particular reason you're trying to pick a fight over a comment? It's an anecdote, a personal observation. The only thing I say in it is "You **might consider** .. if you can." It's just a piece of hard-won advice. How about we leave it at that? – Heretic Monkey Jul 25 '18 at 23:21

1 Answers1

0

Found the solution (kinda?). Still have no idea why it was breaking but when I went into my references and deleted certain assemblies, the issue went away.

For anyone curious, the assemblies I removed were the Owin.Security.Providers assembly, as well as all of it's sub-assemblies.

  • If you feel this is the solution it would be helpful to others if you could list the assemblies. – Jasen Jul 26 '18 at 01:39