简体   繁体   中英

How to pass complex linq result from the controller to view in C# MVC applciation

Developed MVC applciation and Code first approach, had three different class like

First Class

 public class Users
    {
        [Key]
        public int UserID { get; set; }    
        [DisplayName("User Name")]
        public string UserName { get; set; }    
        ...       
        public virtual ICollection<UserDetails> UserDetail { get; set; }
    }

Second Class

   public class UserDetails
    {
        [Key]
        public int UserDetailsID { get; set; }
        [ForeignKey("Users")]
        public int UserID { get; set; }

        [Required(ErrorMessage = "Please enter the first name")]
        [DisplayName("First Name")]
        public string FirstName { get; set; }
        ...
        public virtual Users User { get; set; }
        public virtual ICollection<UserAddress> UAddress { get; set; }
    }

Three Class

   public class UserAddress
    {
        [Key]
        public int UserAddressID { get; set; }
        [ForeignKey("UserDetailsID")]
        public int UserDetailsID { get; set; }

        [DisplayName("Address")]
        public string Address { get; set; }
        ...
        public virtual UserDetails UserDetail{ get; set; }
    }

Need linq result like:

var _result = (from users in objDBMVCSamp.User
               join details in objDBMVCSamp.UserDetail 
               on users.UserID equals details.UserID
               select new
               {
                  users.UserName,
                  users.Password,
                  details.FirstName,...
               }).ToList();

and also need another result like:

  var _result = (from users in objDBMVCSamp.User
                   join details in objDBMVCSamp.UserDetail 
                   on users.UserID equals details.UserID
                   join address in objDBMVCSamp.UserAddress
                   on address.UserDetailsID equals details.UserDetailsID
                   select new
                   {
                      users.UserName,
                      details.FirstName,...
                      details.UserID,
                      address.Address,...
                   }).ToList();

Need to show those result in in view. it's really possible or any other way to show without create a class.

NOTE:

Above result are dynamic, so i though we not able to create a static class for those result. For this scenario what is best method to do this.

If you want to pass the value to view I'd rather suggest to create a ViewModel based upon what you want to show in view.

For example,

ViewModel

public class MyViewModel
{
   public var MyValue1{get;set;}
   public var MyValue2{get;set;}
   public var MyValue3{get;set;}
   ...
   ...
}

in controller

public ActionResult MyControllerMethod(someType MyType)
{
   // setViewmodels value here
}

then in view

@model MyViewModel

<h1>Model.MyValue1</h1> @*this will display value of MyValue1*@

Define a view model that represents what you want to display

public class UserVM
{
  public string UserName { get; set; }
  public string Password { get; set; }
  public string FirstName { get; set; }
  public string LastName { get; set; }
  public string EmailID{ get; set; }
  public int UserID { get; set; }
}

In your controller

var _result = (from users in objDBMVCSamp.User
  join details in objDBMVCSamp.UserDetail 
  on users.UserID equals details.UserID
  select new UserVM
  {
    UserName = users.UserName,
    Password = users.Password,
    FirstName = details.FirstName,
    LastName = details.LastName,
    EmailID = details.EmailID,
    UserID = details.UserID
  }).ToList();
return View(_result)

View

@model List<UserVM>
@for(int i = 0; i < Model.Count; i++)
{
  @Html.DisplayFor(m => m[i].UserName)
  ....

These are the ways of passing data from controller to view

1) You can use the dynamic object ViewBag to pass data from Controllers to Views.

Add the following to your controller:

     ViewBag.MyList = myList;

Then you can acces it from your view:

     @ViewBag.MyList
   // e.g.@foreach (var item in ViewBag.MyList) { ... }

2) Using ajax service call

  $.ajax(
    {
        type: "GET",
        url: "../Home/GetData",
        data: { function_param: values}
    });

3) Use strongly typed classes

   @model MyViewModel
  //e.g.@foreach (var item in MyViewModel) { ... }

For more information, kindly refer this link

http://www.c-sharpcorner.com/UploadFile/abhikumarvatsa/various-ways-to-pass-data-from-controller-to-view-in-mvc/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM