0

Here I have two classes First class is UserEmpolyee.cs

    namespace WebApplication.Models {
    [Table("UserEmployee")]
public partial class UserEmployee
{
    public int ID { get; set; }

    [StringLength(50)]
    public string UserName { get; set; }

    [StringLength(50)]
    public string Password { get; set; }

    public int StoreAccess { get; set; }
    public int CountryAccess { get; set; }
    public int UserLevel { get; set; }  [NotMapped]
    public List<UserEmpEmails> UserEmalsEnumerable { get; set; }
    [NotMapped]
    public List<UserEmployee> UserEmployees { get; set; }

}}   

2nd class is UserEmpEmails.cs

    namespace WebApplication.Models {
[Table("UserEmpEmails")]
public class UserEmpEmails
{
    public int ID { get; set; }
    public string Email { get; set; }
    public int UserEmpID { get; set; }
}}

and controller is UserController

    public ActionResult Index(string Keyword)
    {
        context = new Contoso();
        //if ((int)Session["Level"] > 4) return RedirectToAction("Edit", new { ID = (int)Session["UserID"] });
        int Level = (int)Session["Level"];
        int Access = (int)Session["Access"];
        int userID = (int)Session["UserID"];

        int CountryAccess = (int)Session["CountryAccess"];
        if (Level == 6)
            return View("ChangePassword");
        else if (Level == 5)
            return RedirectToAction("Edit", new { ID = (int)Session["UserID"] });
        else
        {
            var UserList = context.UserEmployees.ToList();
            foreach (UserEmployee user in UserList)
            {
               user.UserEmalsEnumerable = context.UserEmpEmailses.Where(x => x.UserEmpID == user.ID).ToList();
            }
         return View(
            (Keyword == "" ? UserList :
            UserList.Where(x =>
            x.UserName.ToUpper().Contains(Keyword.ToUpper()) ||
            x.FullName.Contains(Keyword.ToUpper()) 
            x.UserEMialsEmpEmails.Email.Contains(Keyword.ToUpper()) 
            ).ToList()).OrderBy(x => x.UserLevel).ThenBy(x => x.UserName).ToList());
    }
}        

Here is my View and in View I am using HTML.Grid

     @model IEnumerable<CostcoWebApplication.Models.UserEmployee>
@using GridMvc.Html
@Html.Grid(Model).Named("UserGrid").Columns(x =>
               {
                   x.Add(y => y.UserName);
                   x.Add(y => y.FullName);
                x.Add(y => y.UserEmalsEnumerable);
                   x.Add().Encoded(false)
                       .Sanitized(false)
                       .SetWidth(20)
                       .RenderValueAs(y => Html.ActionLink(" Edit", "Edit", new { id = y.ID }, new { @class = "btn btn-primary fa fa-chevron-circle-down" }));
               }).WithPaging(20).Sortable(true) 

I am getting everything what I want in @Html.Grid(Model). but I don't know how to get email column in x.Add(y => y.UserEmalsEnumerable);. Please help

1 Answers1

0

You could perhaps use String.Join to join the strings along with LINQ to select just a list of the email strings. Something along the general lines of:

x.Add()
.Titled("Email")
.Encoded(false)
.Sanitized(false)
.SetWidth(250)
.RenderValueAs(d => String.Join(",", d.UserEmailsEnumerable.Select( e => e.Email).ToArray() ) );

Split and join C# string

Or, if you already had what you wanted, it would make the Grid call simpler. Imagine:

var newModel = (from employee in Model
    select new {
    ,ID = employee.ID
    ,UserName = employee.UserName 
    ,Password = employee.Password 

    ,StoreAccess = employee.StoreAccess 
    ,CountryAccess = employee.CountryAccess 
    ,UserLevel = employee.UserLevel 
    ,Emails = String.Join(",", employee.UserEmailsEnumerable.Select( e => e.Email).ToArray() )
    }).ToList();

Would make it so you can just call, if you were swapping over to @Html.Grid(newModel)

x.Add(y => y.Emails)
Greg
  • 2,410
  • 21
  • 26
  • It gave me this error "Expression 'y => Join(",", y.UserEmalsEnumerable.Select(z => z.Email).ToArray())' not supported by grid" – Unexpected_error Sep 21 '17 at 14:20
  • My appologies, looks like you'll need to use Render Value As. Solution updated. See also this example, which shoes RenderValueAs.http://gridmvc.azurewebsites.net/ – Greg Sep 21 '17 at 14:24
  • https://gridmvc.codeplex.com/wikipage?title=Custom%20columns Also shows render value as – Greg Sep 21 '17 at 14:28
  • You can also break this problem up into two parts. Getting the value you want to display, and then later just displaying it. – Greg Sep 21 '17 at 14:30