views:

50

answers:

1
A: 

Here is what I have in my controller.

            var query = new DetailViewModel
        {
            Lang = _db.Language.ToList(),
            ViewDet = (from gh in _db.Grp
                       select new MultiDetailViewModel
                       {
                           PKey = gh.GrpID,
                           Txts = (from lang in _db.Language
                                  join gtxts in gh.GrpText on lang.LangID equals gtxts.LangID into jointxt
                                  from fintxt in jointxt.DefaultIfEmpty()
                                  select new MultiLangObject
                                  {
                                      LangId = lang.LangID,
                                      Txt =  fintxt != null ? fintxt.GrpTxt : ""
                                  })
                       }).ToList()
        };

Here is my View model.

    public class DetailViewModel
{
    public List<MultiDetailViewModel> ViewDet { get; set; }
    public List<Language> Lang { get; set; }
}

public class MultiDetailViewModel
{
    public int PKey { get; set; }
    public IEnumerable<MultiLangObject> Txts { get; set; }
}

public class MultiLangObject
{
    public string Txt { get; set; }
    public int LangId { get; set; }
}


Here is my View.

<table class="tblMult center">
        <thead>
            <tr>    
            <% foreach (var lang in Model.Lang)
               { %>
               <th><%=lang.LangName %></th>
            <%} %>
                <th>Action</th>
            </tr>
        </thead>
        <% foreach (var g in Model.ViewDet)
           {%>
            <tr>
                <% foreach (MultiLangObject gtxt in g.Txts)
                    {%>
                    <td> 
                        <%if (gtxt != null)
                          {%>
                               <%: gtxt.Txt%>
                        <%}else{ %>

                        <%} %>

                    </td>
                <%} %>
                <td>
                    <a onclick="CallAction('/Group/EditForm/<%= g.PKey %>',event)">Edit</a>

                    <%= Html.ActionLink("Manage", "/Grpdesc/", new { id = g.PKey })%>
                    <%= Html.ActionLink("Delete", "Delete", new { id = g.PKey })%>
                </td>
            </tr>
        <%} %>
    </table>


If someone have a better way to do this please, post.
As bastijn said, I dont think encapsulate View Model into A View Model is the right thing to do.

Jean-Francois