MVC 2视图模型数据问题

发布于 2024-09-14 15:49:34 字数 2052 浏览 4 评论 0原文

这是我的数据库结构

语言 LangID PK
LangName nvarchar(100)

类别
CatID PK
IsActive 位

CategoryText
猫ID FK 猫名 nvarchar(200)
LangID Int

语言
语言ID |语言名称
1 |英语
2 |法语

类别
猫ID |处于活动状态
1 |真实
2 |真实
3 |真实

类别文本
猫ID |猫名 |语言ID
1 |汽车 |1
1 |自动 |2
2 |聊天 |2
3 |飞机| 1
3 |亚飞 | 2


我使用 MVC 2 和框架实体作为 ORM。
首先,我想要一个像 html 表中那样的渲染视图

英语 |法语
汽车 |自动
空|聊天
飞机| Avion

这是我的 Linq 查询:

 var qry6 = from Cat in _db.Category
                   select new CategoryViewModel
                   {
                       Category = Cat,
                       CatTxt = from lang in _db.Language
                               join ctxt in Cat.CategoryText on lang.LangID equals ctxt .LangID into jointxt
                               from endtxt in jointxt.DefaultIfEmpty()
                               select endtxt 

                   };

我的问题是,如果我想在视图中添加其他内容,我不能,因为我的 ViewModel 封装在 Linq 循环中...
我的意思是:

 var qry6 = new CategoryViewModel {
                    IDontKnowWhatTypeINeedHere = (from Cat in _db.Category
                                                  select new 
                                                  {
                                                       Category = Cat,
                                                       CatTxt = from lang in _db.Language
                                                               join ctxt in Cat.CategoryText on lang.LangID equals ctxt .LangID into jointxt
                                                               from endtxt in jointxt.DefaultIfEmpty()
                                                               select endtxt 

                                                   }).ToList(),
                   LangList = _db.Language
        };

像我在示例中所做的那样,将 ViewModel 放入 View Model 中是个好主意吗?

谢谢

Here is my database Structure

Languages
LangID PK
LangName nvarchar(100)

Category
CatID Pk

IsActive Bit

CategoryText
CatID FK
CatName nvarchar(200)
LangID Int

Language
LangID | LangName
1 | English
2 | French

Category
CatID | IsActive
1 | True
2 | True
3 | True

CategoryText
CatID | CatName | LangID
1 | Car |1
1 | Auto |2
2 | Chat |2
3 | Plane | 1
3 | Avion | 2

I Use MVC 2 with Framework Entity as ORM.
First,I would like to have a render view like That in a html Table

English | French
Car | Auto
NULL | Chat
Plane| Avion

Here is my Linq Query:

 var qry6 = from Cat in _db.Category
                   select new CategoryViewModel
                   {
                       Category = Cat,
                       CatTxt = from lang in _db.Language
                               join ctxt in Cat.CategoryText on lang.LangID equals ctxt .LangID into jointxt
                               from endtxt in jointxt.DefaultIfEmpty()
                               select endtxt 

                   };

My problem is if I want to add something else in the View, I Can't because my ViewModel is encapsulate in a Linq Loop...
Here is what I Mean:

 var qry6 = new CategoryViewModel {
                    IDontKnowWhatTypeINeedHere = (from Cat in _db.Category
                                                  select new 
                                                  {
                                                       Category = Cat,
                                                       CatTxt = from lang in _db.Language
                                                               join ctxt in Cat.CategoryText on lang.LangID equals ctxt .LangID into jointxt
                                                               from endtxt in jointxt.DefaultIfEmpty()
                                                               select endtxt 

                                                   }).ToList(),
                   LangList = _db.Language
        };

Is it a good idea to put a ViewModel into a View Model as I do in the example.

Thanks

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

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

发布评论

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

评论(1

居里长安 2024-09-21 15:49:34

这是我的控制器中的内容。

            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()
        };

这是我的视图模型。

    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; }
}

这是我的观点。

<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>

如果有人有更好的方法,请留言。
正如 bastijn 所说,我认为将视图模型封装到视图模型中不是正确的做法。

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.

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