2010-05-07 50 views
3

ASP.NET MVC的CheckBox問題

命名空間MvcApplication1.Models { 公共類GroupRepository { EgovtDataContext分貝=新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(); 
    } 

} 

}

控制器

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

<% } %> 

的事情是,它是不是能夠找到組ID,並顯示以下錯誤。解決辦法是什麼?

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?)
+0

FindGroups()返回什麼?是否有一些HTML缺失? – hunter 2010-05-07 19:19:18

+0

public IQueryable FindGroups() { 從組中返回FindAllGroups() orderby組 select Group; } – maztt 2010-05-07 19:21:42

+0

什麼是Group的命名空間?我猜它不是'System.Text.RegularExpressions.Group' – hunter 2010-05-07 19:22:24

回答

3

嘗試使用類型的FindGroups()的命名空間使用像這樣:

<% foreach (var i in ViewData["Group"] as List<MyNamespace.Blah.Group>) 
    { %> 
     <input type="checkbox" name="Inhoud" 
       value="<%= i.int_GroupId %>" checked="checked" /> 
<% } %> 

或添加一個命名空間引用到Web.config或命名空間添加到您的網頁標題。我想你仍然會與`System.Text.RegularExpressions'發生名字空間衝突。

MVC風格看與LINQ

(ViewData["Group"] as List<MyNamespace.Blah.Group>) 
    .ForEach(g => Response.Write(
     Html.CheckBox("Inhoud", true, new { value = g.int_GroupId }))); 
+0

'.ForEach'從哪裏來?這是你在IEnumerable上添加的擴展方法嗎? – 2010-05-07 19:32:30

+0

哦,對不起。這裏:http://msdn.microsoft.com/en-us/library/bwabdf9z.aspx。非常好。 – 2010-05-07 19:35:26

+0

謝謝!我不知道它是否更具可讀性,但它確實看起來很漂亮,並呈現一個複選框,MVC是如何讓你看到的。 – hunter 2010-05-07 19:42:23

1

我覺得你有一個參考的問題,在ASPX代碼相信你正在談論System.Text.RegularExpressions.Group,而不是集團的您從ActionResult,其中返回式您使用Group你需要確保它是你想要的Group,或者刪除使用爲System.Text.RegularExpressions命名空間,或者如果你需要,完全限定Group與您的命名空間

1

糾正我,如果我錯了,但它容貌像這樣可能是namespace issueGroupSystem.Text.RegularExpressions.Group的範圍內,我猜你在你的回購站中有一個名爲Group的表。嘗試將命名空間放入forloop中的類型聲明中,這是您的回購,因此它不會將它與System.Text.RegularExpressions.Groupnamespace混淆。

2

包括你爲你的組類的命名空間,以你的web.config頁/命名空間

<pages> 
    <namespaces> 
     ... 
     <add namespace="Com.Example.Foo.Interfaces" /> 
     <add namespace="Com.Example.Foo.Common" /> 
     ... 
    </namespaces> 
    </pages 

使用視圖特定的模型,而不是通用的ViewData,並通過組作爲模型的屬性,因此它能夠正確地確定方式。

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