日期:2014-05-17  浏览次数:20457 次

asp.net mvc中的IEnumerable问题
如题,我有个账户表和一个账户类型表

账户表:AccountID AccountTypeID AccountName
账户类型表:AccountTypeID  AccountTypeName

两个表用AccountTypeID作为外键,现在要在Index页面中显示如下信息

账户名称    账户类型
zhangsan    保险
lisi        短信

控制器代码如下:

public ActionResult Index()
        {
            var accounts = accountService.GetAccounts();    //查询所有账户信息,返回IEnumerable<Account>集合
            if (accounts != null && accounts.Count() > 0)
            {
                return View(accounts);
            }
            else
                return View();
        }

页面片段代码:

@model IEnumerable<JBD.Domain.Account>
foreach (var item in Model)
{
   <td>
       @item.AccountName
   </td>
   <td>
       @item.AccountTypeID
   </td>
}


都知道这样只能显示账户类型编号,请问要在控制器中怎么写才能在页面中显示账户类型名称呢?

------解决方案--------------------
你可以在后台通过ID先找到AccountTypeName 存起来,页面里调用它

     ViewData["AccountTypeName"] = AccountTypeName;

------解决方案--------------------
帮你顶下,另外这个 我没看懂你想问的是什么 
------解决方案--------------------
引用:
你可以在后台通过ID先找到AccountTypeName 存起来,页面里调用它

C# code


     ViewData["AccountTypeName"] = AccountTypeName;
LZ你把这些都分类以后存放到不同的ViewData中
------解决方案--------------------
引用:
你可以在后台通过ID先找到AccountTypeName 存起来,页面里调用它

C# code

     ViewData["AccountTypeName"] = AccountTypeName;

------解决方案--------------------
也是没看懂想问什么...
------解决方案--------------------
controller别返回IEnumerable<Account>,直接把AccountName和AccountTypeName存到ViewData中。
不过貌似这样不是最好的解决办法,等高人指点。
------解决方案--------------------
修改你的model把账户类型作为账户的导航属性

model里
。。。
public virtual ICollection<账户类型> 账户类型 { get; set; }
。。。