Why am I getting ArgumentOutOfRangeException

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


Why am I getting ArgumentOutOfRangeException



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





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
yesterday


foreach





Oh yes, my apologies, didn't think that comment through before submitting it, it's a List<Lens>
– Matthew Gower
yesterday





And how are you sending the model to the view? Please show us that code. We need a Minimal, Complete, and Verifiable example. I don't think Lenses contains what you think it contains.
– Amy
yesterday


Lenses





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
yesterday




Controller





You might consider using return View(lQuery.GetAll()); and using an @model List<Lens> 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
yesterday


return View(lQuery.GetAll());


@model List<Lens>


ViewBag


ViewBag




1 Answer
1



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.


Owin.Security.Providers





If you feel this is the solution it would be helpful to others if you could list the assemblies.
– Jasen
yesterday






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Keycloak server returning user_not_found error when user is already imported with LDAP

PHP parse/syntax errors; and how to solve them?

How to scale/resize CVPixelBufferRef in objective C, iOS