I currently have my “DomainModel” which handles all the SQL data through EDMX.
When I used an ASPX page I was able to use the following code:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<DomainModel.aspnet_Membership>>" %>
<% foreach (DomainModel.aspnet_Membership member in Model)
{
List<DomainModel.aspnet_Roles> roles = member.aspnet_Users.aspnet_Roles.ToList();
}
This returned the list of all the members from the SQL DB via domainModel.
That was for my test though, now it is a requirement that I use CSHTML razor pages. (Using “@” code blocks)
I tried to implement similar code, but It always falls over:
@model IEnumerable<DomainModel.aspnet_Membership>
@foreach (DomainModel.aspnet_Membership m in Model)
{
<b>@m.aspnet_Users.UserName</b>
}
edit: error: NullReferenceException was unhandled. Obj Reference not set to instance of an object
Controller (I forgot to add this code in, now the same error occurs, but in the controller)
private productName_SATEntities _context; [HttpParamAction] [AcceptVerbs(HttpVerbs.Post)] public ActionResult Scenario() { return View(_context.SAT_Scenarios.ToList()); } _Context links to the content generated by the EDMX
Should not it be:
? I added braces (
{and}) aroundforeachstatement. Otherwise compiler would think that you need a value offoreachon your page, which is a compile-time error.I also added parenthesis (
(and)) aroundm.aspnet_Users.UserNameto make live easier: it should work without them, but with then we implicitly control what compiler will consider server-side code (it will definitely not break at_, for instance).