当前位置:编程学习 > C#/ASP.NET >>

MVC2的问题.

控制层  
public ActionResult Index()
        {
            string str = System.Configuration.ConfigurationManager.ConnectionStrings["MainConnectionString"].ToString();

            DataClasses1DataContext dc = new DataClasses1DataContext(str);

            var blogEntity = from A in dc.COL select new { A.COLName };
            return View(blogEntity.ToList());  
        }
视图层
<% foreach (var item in Model) { %>
    
        <tr>
            
            <td>
                <%: item.COLName%>
            </td>
            
        </tr>
    
    <% } %>
这些写出错。提示“object”未包含“COLName”的定义

把控制写成下面这个就可以
public ActionResult Index()
        {
            string str = System.Configuration.ConfigurationManager.ConnectionStrings["MainConnectionString"].ToString();

            DataClasses1DataContext dc = new DataClasses1DataContext(str);

            var blogEntity = from A in dc.COL select A;
            return View(blogEntity.ToList());  
        }
--------------------编程问答-------------------- 顶~~~~~! --------------------编程问答-------------------- select new { COLName = A.COLName } --------------------编程问答-------------------- select new { COLName = A.COLName }也是一样的错误 --------------------编程问答-------------------- model层没定义COLName
或者select语句中没有COLName --------------------编程问答-------------------- var blogEntity = from A in dc.COL select new { A.COLName };
  return View(blogEntity.ToList());

这样写,返回的是dc.COL的一个字段的集合,
如果这个字段是string类型,可以看作是返回List<string>,
如果这个字段是ing型,可以看作是返回List<int>
以此类推

在View中你去遍历List<string>当然不会包括XXX的定义

var blogEntity = from A in dc.COL select A;
  return View(blogEntity.ToList()); 
这样写,返回的是dc.COL的一个集合
如果dc.COL是一个实体类集合,可以看作是返回List<实体类>,当然可以在View中通过该实体遍历并获取该实体的XXX字段 --------------------编程问答--------------------
引用 5 楼 hrabeyond 的回复:
var blogEntity = from A in dc.COL select new { A.COLName };
  return View(blogEntity.ToList());

这样写,返回的是dc.COL的一个字段的集合,
如果这个字段是string类型,可以看作是返回List<string>,
如果这个字段是ing型,可以看作是返回List<int>
以此类推

……

up --------------------编程问答--------------------
引用 2 楼 karascanvas 的回复:
select new { COLName = A.COLName }

正解! --------------------编程问答-------------------- 你传递到视图层的一个字符串数组,所以Model中每个值都是一个字符串,直接用 <%: item%> 就行了,不是<%: item.COLName%> --------------------编程问答-------------------- 视图层
<% foreach (var item in Model) { %>
    
  <tr>
    
  <td>
  <%: item%>
  </td>
    
  </tr>
    
  <% } %>

页面显示效果是
{ COLName = dsf } 
{ COLName = aaa } 
怎么让它显示
dsf
aaa --------------------编程问答-------------------- var blogEntity = from A in dc.COL select new { A.COLName };
  return View(blogEntity.ToList());
怎么样这个也返回一个实体呢 --------------------编程问答-------------------- 页面显示效果是
{ COLName = dsf } 
{ COLName = aaa }  
???????
--------------------编程问答-------------------- 视图层
<% foreach (var item in Model) { %>
    
  <tr>
    
  <td>
  <%: item%>
  </td>
    
  </tr>
    
  <% } %>

页面中显示是
{ COLName = dsf }  
{ COLName = aaa }  
怎么让它显示成
dsf
aaa --------------------编程问答-------------------- var blogEntity = from content in db.CMS_Content
                             join category in db.CMS_Category on
                                 content.CategoryID equals category.ID
                             join user in db.CMS_UserInfo on
                             content.UserID equals user.ID
                             where content.ID == id && content.TopRank == 0
                             orderby content.TopRank descending, content.ID
                             select new
                             {
                                 content.ID,
                                 content.Title,
                                 UserName = user.DisplayName,
                                 CategoryName = category.Name,
                                 content.CreateDate,
                                 content.ModifyDate,
                                 content.Content
                             };
            return blogEntity.ToList();
像这种多表查询应该怎么返回呢
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,