3

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 of type 'System.Text.RegularExpressions.Group' could be found 
 (are you missing a using directive or an assembly reference?)
3
  • what does FindGroups() return? Is there some html missing? Commented May 7, 2010 at 19:19
  • public IQueryable<Group> FindGroups() { return from Group in FindAllGroups() orderby Group select Group; } Commented May 7, 2010 at 19:21
  • what is the namespace of Group? I'm guessing its not System.Text.RegularExpressions.Group Commented May 7, 2010 at 19:22

4 Answers 4

3

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 })));
Sign up to request clarification or add additional context in comments.

4 Comments

Where is the .ForEach coming from? Is that an extension method you have tacked onto IEnumerable?
thanks! I don't know if it's more readable but it sure looks pretty and renders a checkbox how MVC wants you to.
well it worked ,Why it work with var and doesn't work in group
2

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" /> 
<% } %>  

Comments

1

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

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.