开发者

How to print after join linq query

开发者 https://www.devze.com 2023-02-17 08:51 出处:网络
I have this code: public ActionResult Index() { MembershipUser currentUser = Membership.GetUser(); Guid UserId = (Guid)currentUser.ProviderUserKey;

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,
   开发者_JAVA技巧                    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?


    //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);


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>


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消