显示数据库中的用户配置文件

时间:2011-12-16 11:34:56

标签: asp.net asp.net-mvc linq-to-sql

我已经尝试创建登录表单,并且还希望显示我已存储在数据库中的用户登录配置文件。但是,我不知道如何从模型和控制器中做到这一点。

我已将所有用户信息存储在我的数据库的Customers表中。

这是客户模型的代码:

    public IList<Customer> GetProfileCustomer(string name)
    {
        var list_customer = from c in DataContext.Customers
                            where c.WebAccount == name
                            select c;
        return list_customer.ToList();
    }

这是我的AccountController:

    public ActionResult ShowProfile()
    {
      return View();
    }

我希望得到我在模型中写的结果,以便在我的视图中显示。

1 个答案:

答案 0 :(得分:3)

将函数的结果传递给ViewModel:

public ActionResult ShowProfile()
    {
      ViewData.Model = GetProfileCustomer("foo");
      return View();
    }

然后您的视图将声明@model IList<Customer>,您将像这样访问属性:

@foreach (var customer in Model)
{
<p>@customer.FirstName</p>
}
相关问题