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;
}
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.
Related
This is probably a very simple problem, but I am extremely new to C# / MVC and I have been handed a broken project to fix. So it's time to sink or swim!
I have an array of strings that is being passed from a function to the front end.
The array looks something like
reports = Directory.GetFiles(#"~\Reports\");
On the front end, I would like it to display each report, but I am not sure how to do that.
This project is using a MVC, and I believe the view is called "Razor View"? I know that it's using an HTML helper.
In essence, I need something like
#HTML.DisplayTextFor(Model.report [And then print every report in the array]);
I hope that makes sense.
If you want to display the file name array you can simply use a foreach:
#foreach(var report in Model.Reports){ #report }
Note that you should add the Reports property to your view model:
public class SampleViewModel
{
public string [] Reports { get; set; }
}
You could use ViewData or TempData but I find that using the view model is the better way.
You can then populate it:
[HttpGet]
public ActionResult Index()
{
var model = new SampleViewModel(){ Reports = Directory.GetFiles(#"~\Reports\")};
return View(model);
}
And use it in the view as you see fit.
Here is a simple online example: https://dotnetfiddle.net/5WmX5M
If you'd like to add a null check at the view level you can:
#if(Model.Reports != null)
{
foreach(var report in Model.Reports){ #report <br> }
}
else
{
<span> No files found </span>
}
https://dotnetfiddle.net/melMLW
This is never a bad idea, although in this case GetFiles will return an empty list if no files can be found, and I assume a possible IOException is being handled.
So...i created a Sharepoint Add-in (C# MVC) to get list information and create/update items. I've done the creating/updating in the past, not gonna tackle that now.
My problem here is displaying the list items into the MVC View. What i've done so far ->
I created a model (class) with the information that i'll show in the table:
public class IRFItem
{
public string Title { get; set; }
public string StartDate { get; set; }
public string EndDate { get; set; }
//public string CreatedBy { get; set; }
}
In the same file (for the sake of keeping my tests compact) i also added a way to get the items i need:
public static List<IRFItem> GetItems(SharePointContext spContext, CamlQuery camlQuery)
{
List<IRFItem> items = new List<IRFItem>();
//var spContext = SharePointContextProvider.Current.GetSharePointContext(HttpContext.Current);
using (var clientContext = spContext.CreateUserClientContextForSPAppWeb())
{
if (clientContext != null)
{
List irfList = clientContext.Web.Lists.GetByTitle("IRF");
ListItemCollection irfListItems = irfList.GetItems(camlQuery);
clientContext.Load(irfListItems);
clientContext.ExecuteQuery();
if (irfListItems != null)
{
foreach (var irfListItem in irfListItems)
{
items.Add(
new IRFItem
{
Title = irfListItem["Title"].ToString(),
StartDate = irfListItem["StartDate"].ToString(),
EndDate = irfListItem["EndDate"].ToString(),
});
}
}
else
{
items.Add(
new IRFItem
{
Title = "Empty",
StartDate = "Empty",
EndDate = "Empty"
});
}
}
}
return items;
}
In my custom controller (called SharepointController so i dont mess up with the default ones) i added this ->
// GET: Sharepoint
[SharePointContextFilter]
public ActionResult Index()
{
var spContext = SharePointContextProvider.Current.GetSharePointContext(HttpContext);
ViewBag.Username = SharePointService.GetUserName(spContext);
CamlQuery queryProducts = new CamlQuery();
queryProducts.ViewXml =
#"<View>
<ViewFields><FieldRef Name='StartDate' /><FieldRef Name='LinkTitle' /><FieldRef Name='Title' /><FieldRef Name='Author' /><FieldRef Name='EndDate' /><FieldRef Name='ID' /></ViewFields>
</View>";
List<IRFItem> items = SharePointService.GetItems(spContext, queryProducts);
//SharePointService.GetItems(spContext, queryProducts);
return View(items);
}
And finally my desired view contains->
#foreach (AddinTest1Web.Models.IRFItem irfItem in Model)
{
<tr>
<td>#irfItem.Title</td>
<td>#irfItem.StartDate</td>
<td>#irfItem.EndDate</td>
</tr>
}
I get no errors, but my table is always empty...I even added that else part in GetItems to create an item that shows empty just so i know if its a sharepoint related problem or something else. My MVC experience isn't much to be honest (just started learning it a week ago, but im the kind of guy that learns by doing).
Does anyone see any problems here? I've followed a tutorial for this and made my own little changes.
Ref: https://www.chakkaradeep.com/2013/10/18/building-apps-for-sharepoint-with-mvc/
Any kind of tip will be highly appreciated, thx.
EDIT: I jumped over the error by giving the application more permissions (to list & web just to be safe) and i am getting back results, however i am unable to create the items because executeQuery does not finish on time. Any idea how to delay that? I remember i had a bigggg problem with tasks in the past so i have no idea where to start here.
Couple of things I see from debugging.
I would move the code GetItems() to separate Helper class and then put a breakpoint in it to see if it is firing.
If you are not getting any error and return data is always empty, then that means that some part of the code is not getting executed (Sync or async issue?)
Ok so i fixed the problem (been fighting with this for hours and it was so damn simple i wanna punch myself right now).
In case anyone encounters this ->
Why my list didnt get any items (even tho no erorrs) -> Not enough permissions given to the app. Fixed by giving full list permissions and full web permissions.
Why my foreach was giving an error -> Apparently one column had a different name. I figured that out by trying this code (which apparently is better cause it throws an error, unlike getting everything from the list) ->
clientContext.Load(irfListitems,
itemss => itemss.Include(
item => item["Title"],
item => item["StartDate"],
item => item["EndDate"]
));
Following some major refactoring (moving to a PCL) I have some code (which was not part of the refactor) that was running fine but is now throwing exceptions.
The code is part of a Xamarin Android project which was using File Linking before the move to a Portable Class Library.
It's a simple thing but I can't see why this would happen
foreach(var station in stationList)
{
// Breakpoint on next line shows station to be null..!
if(station.ClusterId != Guid.Empty)
{
// Code in here
}
}
The problem is that although the stationList contains a number of StationViewModel objects the station instance is always null - how can that be?
I have tried replacing the foreach with a for loop but the result was the same - station was null.
I've also restarted Visual Studio and rebooted.
No Xamarin updates appear to be outstanding.
The code was running fine and the generation of the stationList has not changed nor has the implementation of this class.
EDIT:
The stationList creation process is:
Call made to SQLite 'repo' in PCL which returns IList<station> (which is populated)
_loadedStations = await _stationManager.GetStationsAsync();
Using AutoMapper a new List<StationViewModel> is generated from the above list (which is populated correctly)
fullStationList = AutoMapper.Mapper.Map<IList<Station>, IList<StationViewModel>>(_loadedStations);
In a separate method the view model list above is filtered based on the LatLng coordinates.
var stationList = fullStationList.Where(x => mapBounds.Contains(new LatLng(x.Latitude, x.Longitude))).ToList();
The foreach follows the above line of code..
SOLUTION:
Well I've 'solved' the problem but still don't know what caused it.
In the same method as the foreach there is another, contained within an if. It too has the station identifier;
if (zoomChanged)
{
foreach (var station in fullStationList)
{
station.ClusterId = Guid.Empty;
}
RunOnUiThread(() => _stationMap.Clear());
_clusters.Clear();
}
By changing either of the variable names the code will run fine and the previously erroring loop will run without any problem.
Note that this 2nd loop was not within the 1st one - that's obviously
not going to work, but I can't see why this was causing a problem.
It seems like this has something to do with the way Xamarin works , changing var name solves the issue
foreach(var stationItem in stationList)
{
// Breakpoint on next line shows station to be null..!
if(stationItem.ClusterId != Guid.Empty)
{
// Code in here
}
}
I tried it in this way and it works:
internal class CStation
{
private Guid _clusterId;
public CStation()
{
_clusterId = Guid.NewGuid();
}
public Guid StationName
{
get { return _clusterId; }
}
}
private voit TestList()
{
List<CStation> stationList = new List<CStation>();
CStation test1 = new CStation();
CStation test2 = new CStation();
CStation test3 = new CStation();
stationList.Add(test1);
stationList.Add(test2);
stationList.Add(test3);
foreach (var station in stationList)
{
// Breakpoint on next line shows station to be null..!
if (station == null )
{
throw new ArgumentNullException();
}
}
}
I think you didnt instantiate the Guid.
I did it in the constructor of Station -> _clusterId = Guid.NewGuid();
I can't sort this weird issue out and I have tried anything and everything I can think of.
I got 5 pages, everyone of them passing variables with navigation this way:
Pass:
NavigationSerice.Navigate(new Uri("/myPage.xaml?key=" + myVariable, UriKind.Relative));
Retrieve:
If (NavigationContext.QueryString.ContainsKey(myKey))
{
String retrievedVariable = NavigationContext.QueryString["myKey"].toString();
}
I open a list on many pages and one of the pages automatically deletes an item from the list actualProject (actualProject is a variable for a string list). Then, when I go so far back that I reach a specific page - the app throws an exception. Why? I have no idea.
The code that deletes the item:
// Remove the active subject from the availible subjects
unlinkedSubjects.Remove(actualSubject);
unlinkedsubjectsListBox.ItemsSource = null;
unlinkedsubjectsListBox.ItemsSource = unlinkedSubjects;
Then the page that throws the exception's OnNavigatedTo event:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
if (NavigationContext.QueryString.ContainsKey("key"))
{
actualProject = NavigationContext.QueryString["key"];
try
{
//Read subjectList from IsolatedStorage
subjectList = readSetting(actualProject) != null ? (List<String>)readSetting(actualProject) : new List<String>();
//Put the subjectList into the subjectListBox
subjectListBox.ItemsSource = subjectList;
//Set the subjectsPageTitle to the "actualProject" value, to display the name of the current open project at the top of the screen
subjectsPageTitle.Text = actualProject;
}
catch (Exception)
{
if (language.Equals("en."))
{
// Language is set to english
MessageBox.Show("Couldn't open the project, please try again or please report the error to Accelerated Code - details on the about page");
}
else if (language.Equals("no."))
{
// Language is set to norwegian
MessageBox.Show("Kunne ikke åpne prosjektet, vennligst prøv igjen eller rapporter problemet til Accelerated Code - du finner detaljer på om-siden");
}
}
}
}
Exception:
_exception {System.ArgumentException: Value does not fall within the expected range.} System.Exception {System.ArgumentException}
My theory:
The app kind of loads the currently opened and modified List. Is that possible? No idea.
So there are a number of ways to pass data between pages.
The way you have chosen is the least suggested.
You can use the PhoneApplicationService.Current dictionary but this is messy also if you have a ton of variables, doesn't persist after app shut down and could be simplified.
I wrote a free DLL that kept this exact scenario in mind called EZ_iso.
You can find it here
Basically what you would do to use it is this.
[DataContractAttribute]
public class YourPageVars{
[DataMember]
public Boolean Value1 = false;
[DataMember]
public String Value2 = "And so on";
[DataMember]
public List<String> MultipleValues;
}
Once you have your class setup you can pass it easily between pages
YourPageVars vars = new YourPageVars { /*Set all your values*/ };
//Now we save it
EZ_iso.IsolatedStorageAccess.SaveFile("PageVars",vars);
That's it! Now you can navigate and retrieve the file.
YourPageVars vars = (YourPageVars)EZ_iso.IsolatedStorageAccess.GetFile("PageVars",typeof(YorPageVars));
This is nice because you can use it for more than navigation. You can use it for anything that would require Isolated storage. This data is serialized to the device now so even if the app shuts down it will remain. You can of course always delete the file if you choose as well.
Please make sure to refer to the documentation for any exceptions you have. If you still need help feel free to hit me up on twitter #Anth0nyRussell or amr#AnthonyRussell.info
I am building in a Change History / Audit Log to my MVC app which is using the Entity Framework.
So specifically in the edit method public ActionResult Edit(ViewModel vm), we find the object we are trying to update, and then use TryUpdateModel(object) to transpose the values from the form on to the object that we are trying to update.
I want to log a change when any field of that object changes. So basically what I need is a copy of the object before it is edited and then compare it after the TryUpdateModel(object) has done its work. i.e.
[HttpPost]
public ActionResult Edit(ViewModel vm)
{
//Need to take the copy here
var object = EntityFramework.Object.Single(x=>x.ID = vm.ID);
if (ModelState.IsValid)
{
//Form the un edited view model
var uneditedVM = BuildViewModel(vm.ID); //this line seems to confuse the EntityFramework (BuildViewModel() is used to build the model when originally displaying the form)
//Compare with old view model
WriteChanges(uneditedVM, vm);
...
TryUpdateModel(object);
}
...
}
But the problem is when the code retrieves the "unedited vm", this is causing some unexpected changes in the EntityFramework - so that TryUpdateModel(object); throws an UpdateException.
So the question is - in this situation - how do I create a copy of the object outside of EntityFramework to compare for change/audit history, so that it does not affect or change the
EntityFramework at all
edit: Do not want to use triggers. Need to log the username who did it.
edit1: Using EFv4, not too sure how to go about overriding SaveChanges() but it may be an option
This route seems to be going nowhere, for such a simple requirement! I finally got it to override properly, but now I get an exception with that code:
public partial class Entities
{
public override int SaveChanges(SaveOptions options)
{
DetectChanges();
var modifiedEntities = ObjectStateManager.GetObjectStateEntries(EntityState.Modified);
foreach (var entry in modifiedEntities)
{
var modifiedProps = ObjectStateManager.GetObjectStateEntry(entry).GetModifiedProperties(); //This line throws exception The ObjectStateManager does not contain an ObjectStateEntry with a reference to an object of type 'System.Data.Objects.EntityEntry'.
var currentValues = ObjectStateManager.GetObjectStateEntry(entry).CurrentValues;
foreach (var propName in modifiedProps)
{
var newValue = currentValues[propName];
//log changes
}
}
//return base.SaveChanges();
return base.SaveChanges(options);
}
}
IF you are using EF 4 you can subscribe to the SavingChanges event.
Since Entities is a partial class you can add additional functionality in a separate file. So create a new file named Entities and there implement the partial method OnContextCreated to hook up the event
public partial class Entities
{
partial void OnContextCreated()
{
SavingChanges += OnSavingChanges;
}
void OnSavingChanges(object sender, EventArgs e)
{
var modifiedEntities = ObjectStateManager.GetObjectStateEntries(EntityState.Modified);
foreach (var entry in modifiedEntities)
{
var modifiedProps = ObjectStateManager.GetObjectStateEntry(entry.EntityKey).GetModifiedProperties();
var currentValues = ObjectStateManager.GetObjectStateEntry(entry.EntityKey).CurrentValues;
foreach (var propName in modifiedProps)
{
var newValue = currentValues[propName];
//log changes
}
}
}
}
If you are using EF 4.1 you can go through this article to extract changes
See FrameLog, an Entity Framework logging library that I wrote for this purpose. It is open-source, including for commercial use.
I know that you would rather just see a code snippet showing how to do this, but to properly handle all the cases for logging, including relationship changes and many-to-many changes, the code gets quite large. Hopefully the library will be a good fit for your needs, but if not you can freely adapt the code.
FrameLog can log changes to all scalar and navigation properties, and also allows you to specify a subset that you are interested in logging.
There is an article with high rating here at the codeproject: Implementing Audit Trail using Entity Framework . It seems to do what you want. I have started to use this solution in a project. I first wrote triggers in T-SQL in the database but it was too hard to maintain them with changes in the object model happening all the time.