开发者

ASP MVC3 - DomainModel & Razor - displaying info from the database

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_SATEntit开发者_Go百科ies _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:

@model IEnumerable<DomainModel.aspnet_Membership>
@{foreach (DomainModel.aspnet_Membership m in Model)
{
    <b>@(m.aspnet_Users.UserName)</b>
}}

? I added braces ({ and }) around foreach statement. Otherwise compiler would think that you need a value of foreach on your page, which is a compile-time error.

I also added parenthesis (( and )) around m.aspnet_Users.UserName to 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).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜