MVC ASP.net definition GetEnumerator error - asp.net

So i get the error message in the title and i dont understand why.
Heres how i reference the model in the view
#model IEnumerable<Lowflix.Models.LendingIndexModel>
The errors coming up in this foreach
#foreach (var item in Model)
Really cant explain to myself how this i get this error since i even declared IEnumerable. It contains multiple objects.
This is the error message:
foreach statemetn cannot operate on variables
of type "Models" because "Models" does not
contain a public instance of GetEnumerator
-
model:
using Lowflix.Core.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace Lowflix.Models
{
public class LendingIndexModel
{
public LendingIndexModel()
{
}
public Guid LendingId { get; set; }
public Guid CustomerId { get; set; }
public Guid CopyId { get; set; }
public String Title { get; set; }
public DateTime StartDate { get; set; }
public DateTime? ReturnDate { get; set; }
public virtual Movie Movie { get; set; }
public virtual Customer Customer { get; set; }
public virtual Copy Copy { get; set; }
}
debugging model:
model type:
+ Model {System.Linq.Enumerable.WhereSelectListIterator
<Lowflix.Core.Entities.Lending,
Lowflix.Models.LendingIndexModel>} System.Collections.Generic.IEnumerable
<Lowflix.Models.LendingIndexModel>
{System.Linq.Enumerable.WhereSelectListIterator
<Lowflix.Core.Entities.Lending,
Lowflix.Models.LendingIndexModel>}

Please try with below code.
#model List<Lowflix.Models.LendingIndexModel>
In the View page change IEnumerable to List.

Related

How to create a list page for Users in .Net Core 2

We extended the Identity Roles as well Users:
using Microsoft.AspNetCore.Identity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace AthlosifyWebArchery.Models
{
public class ApplicationRole : IdentityRole
{
public ApplicationRole() : base() { }
public ApplicationRole(string roleName) : base(roleName) { }
public ApplicationRole(string roleName, string description, DateTime createdDate) : base(roleName)
{
base.Name = roleName;
this.Description = description;
this.CreatedDate = createdDate;
}
public string Description { get; set; }
public DateTime CreatedDate { get; set; }
}
}
and
using Microsoft.AspNetCore.Identity;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Threading.Tasks;
namespace AthlosifyWebArchery.Models
{
public class ApplicationUser : IdentityUser
{
public ApplicationUser() : base() { }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Address { get; set; }
public string Suburb { get; set; }
public string State { get; set; }
public string Postcode { get; set; }
public string Country { get; set; }
[InverseProperty("ApplicationUser")]
public IList<HostApplicationUser> HostApplicationUsers { get; set; }
[InverseProperty("HostApplicationCreatedUser")]
public HostApplicationUser HostApplicationCreatedUser { get; set; }
[InverseProperty("HostApplicationLastModifiedUser")]
public HostApplicationUser HostApplicationLastModifiedUser { get; set; }
[InverseProperty("ApplicationUser")]
public IList<ClubApplicationUser> ClubApplicationUsers { get; set; }
[InverseProperty("ClubApplicationCreatedUser")]
public ClubApplicationUser ClubApplicationCreatedUser { get; set; }
[InverseProperty("ClubApplicationLastModifiedUser")]
public ClubApplicationUser ClubApplicationLastModifiedUser { get; set; }
}
}
We are trying to create a Razor Page list of users as well their role:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.EntityFrameworkCore;
using AthlosifyWebArchery.Data;
using AthlosifyWebArchery.Models;
using Microsoft.AspNetCore.Identity;
namespace AthlosifyWebArchery.Pages.Administrators.Users
{
public class IndexModel : PageModel
{
private readonly AthlosifyWebArchery.Data.ApplicationDbContext _context;
public IndexModel(AthlosifyWebArchery.Data.ApplicationDbContext context)
{
_context = context;
}
public List<ApplicationUser> User { get; private set; }
public List<IdentityUserRole<string>> UsersRoles { get; set; } // get my roles or context of user
public List<IdentityRole> AllRoles { get; private set; }
public async Task OnGetAsync()
{
User = _context.Users.Include("UserRoles").ToList();
//UsersRoles = _context.UserRoles.ToList(); // get my roles or context of user
//AllRoles = _context.Roles.ToList();
}
}
}
We managed to get just the user list BUT not sure on how to include the Roles in this case.
Any pointer please?
Firstly, try to avoid using Include function's string overload. Using the lamda instead will help you be sure that the property exists. For instance, in this case, a property named UserRoles doesn't exist for your user class in the first place. Secondly, the syntax you are trying to use it generally used for a one to many relationships. Note that users and roles is a many to many relation and the identity context (that your dbcontext extended) has a UserRoles property for this. You should be able to get all users joined with their roles using a query like this:
IEnumerable<User> users = from u in context.Users
from r in context.Roles
from ur in context.UserRoles
where u.Id == ur.UserId && ur.RoleId == r.Id
select u;

add-migration not generating anything

So I've tried running "add-migration AddBooking -verbose"
To this class:
using System;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
namespace HostelBookingSystem.Models
{
public class Booking
{
[Key]
public Int32 BookingId { get; set; }
public Int32 Duration { get; set; }
public Double Price { get; set; }
public BookingStatus Status { get; set; }
public virtual UserProfile UserProfile { get; set; }
public virtual Bunk Bunk { get; set; }
public virtual Room RoomPreference { get; set; }
}
}
However I'm receiving completely empty Up() and Down() methods.
The same thing happens when I remove the 3 public virtual attributes as well.
Can any explain why this is?
1) The DBContext class needs to be set up
2) Include the property for the above class in the DBContext
public DbSet<Booking> Books { get; set; }
The reason this didn't generate anything was because I didn't have a DbContext set up for the class I wished to generate a table for.

Scaffolding with Entity Framework in ASP.NET MVC 4

I'm following along with "Professional ASP.NET MVC 4" and trying to generate a controller from a model using Entity Framework. My Model looks like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
namespace MvcMusicStore.Models
{
public class MusicStoreModels
{
public class Album
{
public virtual int AlbumId { get; set; }
public virtual int GenreId { get; set; }
public virtual int ArtistId { get; set; }
public virtual string Title { get; set; }
public virtual decimal Price { get; set; }
public virtual string AlbumArtUrl { get; set; }
public virtual Genre Genre { get; set; }
public virtual Artist Artist { get; set; }
}
public class Artist
{
public virtual int ArtistId { get; set; }
public virtual string Name { get; set; }
}
public class Genre
{
public virtual int GenreId { get; set; }
public virtual string Name { get; set; }
public virtual string Description { get; set; }
public virtual List<Album> Albums { get; set; }
}
}
}
When I right click my Controllers folder and choose Add > Controller I select "MVC controller with read/write action and views, using Entity Framework" as my template and "Album (MvcMusicStore.Models)" as my Model class. The book tells me to select "new data context..." and name it "MvcMusicStore.Models.MusicStoreDBContext".
Everything looks OK and I have saved and built my solution prior to performing the above actions. However, I get an error message saying
There was an error generating 'MvcMusicStore.Models.MusicStoreDBContext'.
Try rebuilding your project.'
I'm at a bit of a loss. Can anyone help?
The symptoms sound a lot like this issue:
http://www.rhysgodfrey.co.uk/archive/2011/04/20/mvc3-tools-update-and-entity-framework-4-1-error.aspx
I would suggest uninstalling all versions of Entity Framework, re-installing the latest version through NuGet, and regenerating your EF context from scratch.
I not very up on C# (i think it is C# code - but dont quote me on that).....
Well after pasting your code in a new project i notice the line that says
Using System.Data.Entity;
Is flagged as an error,
And some further research shows that a refrence is POSSIBLY missing in inyour config.sys file...
Re: http://forums.asp.net/t/1381740.aspx/1

ForeignKey not being recognised in VS2012 RC

after a lot of help yesterday, I came up against a known error in asp.net4 beta - I upgraded to VS2012 RC Express (4.5), and now VS is reporting two errors in my model, which were ok previously:
"The type or namespace name 'ForeignKeyAttribute' could not be found (are you missing a using directive or an assembly reference?)"
"The type or namespace name 'ForeignKey' could not be found (are you missing a using directive or an assembly reference?)"
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity.ModelConfiguration.Conventions;
using System.Data.Entity;
namespace MvcApplication6.Models
{
public class tblRental
{
[Key()]
public int rental_id { get; set; }
public int room_id { get; set; }
public DateTime check_in { get; set; }
public DateTime check_out { get; set; }
public decimal room_cost { get; set; }
public long customer_ref { get; set; }
[ForeignKey("customer_ref")]
public virtual tblCustomerBooking Customer { get; set; }
}
public class tblCustomerBooking
{
[Key()]
public long customer_id { get; set; }
public string customer_name { get; set; }
public string customer_email { get; set; }
public virtual ICollection<tblRental> Rentals { get; set; }
}
Does anyone know if the ForeignKey reference has been changed?
Thanks for any help,
Mark
I just figured out I needed to add:
using System.ComponentModel.DataAnnotations.Schema;
I didn't need to move it before, so I assume ForeignKey has been moved under the schema namespace.
Hope this helps anyone else,
Thanks, Mark

asp.net json .net objects

I have this class.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace TestConnect
{
public class UserInfo
{
public string id { get; set; }
public string name { get; set; }
public string picture { get; set; }
public string bio { get; set; }
public string quotes { get; set; }
//public HometownInfo from { get; set;
public From hometown { get; set; }
public string relationship { get; set; }
public string gender { get; set; }
public List<Education> data { get; set; }
}
public class Education
{
public From school { get; set; }
public From year { get; set; }
//public From [] concentration { get; set; }
public string type { get; set; }
}
}
public class HometownInfo
{
public string id { get; set; }
public string homename { get; set; }
}
Now trying to do
url = "https://graph.facebook.com/me?fields=education&access_token=" + oAuth.Token;
json = oAuth.WebRequest(oAuthFacebook.Method.GET, url, String.Empty);
Education edu = js.Deserialize<Education>(json);
foreach(Education_history edu_history in Education.data) gives an error
Object reference not set to an
instance of an object. Description: An
unhandled exception occurred during
the execution of the current web
request
Thanks
User
Have you tried setting breakpoints at the various steps in your code and seeing where it might be?
somewhere else in your code you must be declaring url and json?
does js.Deserialize() create a new object or are you missing this
Education edu = new Education();
Also if its in a web application make sure that your code is placed in the correct page events so that they happen in the correct order

Resources