join linq查询后如何打印

发布于 2024-10-22 00:40:41 字数 620 浏览 0 评论 0原文

我有这段代码:

 public ActionResult Index()
{


        MembershipUser currentUser = Membership.GetUser();
        Guid UserId = (Guid)currentUser.ProviderUserKey;
        var users = from m in db.Users
                    join m2 in db.MyProfiles on  m.UserId equals m2.UserId
                    where m.UserId == UserId
                    select new{UserName = m.UserName, LastActivityDate = m.LastActivityDate,
                       Address =  m2.Address, City = m2.City, State = m2.State, Zip = m2.Zip};
        return View(users);

}

这段代码在我的控制器中,我想运行这个查询,然后将结果打印到我的视图中,我将如何编写视图?

I have this code:

 public ActionResult Index()
{


        MembershipUser currentUser = Membership.GetUser();
        Guid UserId = (Guid)currentUser.ProviderUserKey;
        var users = from m in db.Users
                    join m2 in db.MyProfiles on  m.UserId equals m2.UserId
                    where m.UserId == UserId
                    select new{UserName = m.UserName, LastActivityDate = m.LastActivityDate,
                       Address =  m2.Address, City = m2.City, State = m2.State, Zip = m2.Zip};
        return View(users);

}

This code is in my Controller, I want to run this query and then print the results into my view, how would I write the view?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你げ笑在眉眼 2024-10-29 00:40:41
    //if your question is how to display(Print!) a view for above query then in ActionResult Index()
    //1] As as best practise always Create a ViewModel - UserViewModel
      public class UserviewModel
      {
        public string Username {get;set;}
        public string Address {get;set;}
      }

    //2] Assign db.user values to UserviewModel or you can use Automapper

    //and 3] then return this viewmodel to view  
    return View(UserviewModel);
    //if your question is how to display(Print!) a view for above query then in ActionResult Index()
    //1] As as best practise always Create a ViewModel - UserViewModel
      public class UserviewModel
      {
        public string Username {get;set;}
        public string Address {get;set;}
      }

    //2] Assign db.user values to UserviewModel or you can use Automapper

    //and 3] then return this viewmodel to view  
    return View(UserviewModel);
罗罗贝儿 2024-10-29 00:40:41

此代码无法工作,因为您的 LINQ 查询返回匿名对象,因此您无法强类型化您的视图。因此,第一步是定义一个视图模型,它将表示您愿意在视图上显示的信息:

public class UserViewModel
{
    public string UserName { get; set; }
    public DateTime LastActivityDate { get; set; }
    public string Address { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string Zip { get; set; }
}

然后在控制器操作中,您将返回该视图模型的集合:

public ActionResult Index()
{
    var currentUser = Membership.GetUser();
    var userId = (Guid)currentUser.ProviderUserKey;
    var users = 
        from m in db.Users
        join m2 in db.MyProfiles on m.UserId equals m2.UserId
        where m.UserId == userId
        select new UserViewModel
        {
            UserName = m.UserName, 
            LastActivityDate = m.LastActivityDate,
            Address =  m2.Address, 
            City = m2.City, 
            State = m2.State, 
            Zip = m2.Zip
        };
    return View(users);
}

最后在强类型视图中

@model IEnumerable<AppName.Models.UserViewModel>
@Html.DisplayForModel()

:将为集合中的每个项目呈现相应的显示模板 (~/Views/Shared/DisplayTemplates/UserViewModel.cshtml):

@model AppName.Models.UserViewModel
<div>
    Username: @Html.DisplayFor(x => x.UserName)<br/>
    Last activity: @Html.DisplayFor(x => x.LastActivityDate)<br/>
    ...
</div>

This code cannot work because your LINQ query is returning an anonymous object so you cannot strongly type your view. So the first step would be to define a view model which will represent the information you are willing to display on your view:

public class UserViewModel
{
    public string UserName { get; set; }
    public DateTime LastActivityDate { get; set; }
    public string Address { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string Zip { get; set; }
}

then in your controller action you would return a collection of this view model:

public ActionResult Index()
{
    var currentUser = Membership.GetUser();
    var userId = (Guid)currentUser.ProviderUserKey;
    var users = 
        from m in db.Users
        join m2 in db.MyProfiles on m.UserId equals m2.UserId
        where m.UserId == userId
        select new UserViewModel
        {
            UserName = m.UserName, 
            LastActivityDate = m.LastActivityDate,
            Address =  m2.Address, 
            City = m2.City, 
            State = m2.State, 
            Zip = m2.Zip
        };
    return View(users);
}

and finally in your strongly typed view:

@model IEnumerable<AppName.Models.UserViewModel>
@Html.DisplayForModel()

and in the corresponding display template (~/Views/Shared/DisplayTemplates/UserViewModel.cshtml) which will be rendered for each item of the collection:

@model AppName.Models.UserViewModel
<div>
    Username: @Html.DisplayFor(x => x.UserName)<br/>
    Last activity: @Html.DisplayFor(x => x.LastActivityDate)<br/>
    ...
</div>
茶色山野 2024-10-29 00:40:41

您需要获取用户的类型并创建该类型的列表视图。创建视图的最简单方法是在控制器方法中右键单击并选择“创建视图”。这将确保路由也正确完成。

You need to get the type of users and make a List-View of that type. Easiest way to make a view is simply right-clicking in your controller method and selecting Create View. That'll make sure the routing gets done properly as well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文