Repository
namespace MvcApplication1.Models { public class GroupRepository { EgovtDataContext db = new EgovtDataContext();
public IQueryable<Group> FindAllGroups()
{
return db.Groups;
}
public IQueryable<Group> FindGroups()
{
return from Group in FindAllGroups()
orderby Group
select Group;
}
public Group GetGroups(int id)
{
return db.Groups.SingleOrDefault(d => d.int_GroupId == id);
}
//
public void Add(Group group)
{
db.Groups.InsertOnSubmit(group);
}
public void Delete(Group group)
{
db.Groups.DeleteOnSubmit(group);
}
//
// Persistence
public void Save()
{
db.SubmitChanges();
}
}
}
CONTROLLER
public ActionResult Index()
{
GroupRepository grouprepository = new GroupRepository();
ViewData["Group"] = grouprepository.FindGroups();
return View();
}
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master"
Inherits="System.Web.Mvc.ViewPage" %>
<% foreach (Group i in ViewData["Group"] as List<Group>)
{ %>
<input type="checkbox" name="Inhoud"
value="<%= i.int_GroupId %>" checked="checked" />
<% } %>
The thing is that it is not able to find group id and displaying the following error. What is the solution?
CS1061: 'System.Text.RegularExpressions.Group' does not contain a definition for 'int_GroupId' and no extension method 'int_GroupId' accepting a first argument 开发者_如何学Cof type 'System.Text.RegularExpressions.Group' could be found (are you missing a using directive or an assembly reference?)
try using the namespace of the type your FindGroups() uses like so:
<% foreach (var i in ViewData["Group"] as List<MyNamespace.Blah.Group>)
{ %>
<input type="checkbox" name="Inhoud"
value="<%= i.int_GroupId %>" checked="checked" />
<% } %>
or add a namespace reference to your Web.Config or add the namespace to your page header. I think you will still have a namespace conflict with `System.Text.RegularExpressions'.
MVC Style Look with LINQ
(ViewData["Group"] as List<MyNamespace.Blah.Group>)
.ForEach(g => Response.Write(
Html.CheckBox("Inhoud", true, new { value = g.int_GroupId })));
Include your namespace for your Group class to your web.config pages/namespaces
<pages>
<namespaces>
...
<add namespace="Com.Example.Foo.Interfaces" />
<add namespace="Com.Example.Foo.Common" />
...
</namespaces>
</pages
Use a view-specific model instead of generic ViewData and pass Groups as a property of the model so it can properly determine the type.
using Com.Example.Foo.Interfaces;
using Com.Example.Foo.Common;
public class GroupModel
{
public IEnumerable<Group> Groups { get; set; }
}
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master"
Inherits="System.Web.Mvc.ViewPage<GroupModel>" %>
<% foreach (var i in Model.Groups)
{ %>
<input type="checkbox" name="Inhoud"
value="<%= i.int_GroupId %>" checked="checked" />
<% } %>
I think you are having a reference problem, the code in the aspx believes you are talking about System.Text.RegularExpressions.Group
rather than the type of Group you are returning from your ActionResult
, where you use Group
you will need to make sure it is the Group
you want, either remove the using for the System.Text.RegularExpressions
namespace, or if you need that, fully qualify Group
with your namespace
Correct me if I am wrong, but it looks like this may be a namespace issue
.
Group
is in the scope of System.Text.RegularExpressions.Group
, and I am guessing you have a table in your repos that is named Group. Try putting the namespace in for the type declaration in your forloop, that is of your repos so it doesnt confuse it with the System.Text.RegularExpressions.Group
namespace
.
精彩评论