MVC 2视图模型数据问题
这是我的数据库结构
语言 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是我的控制器中的内容。
这是我的视图模型。
这是我的观点。
如果有人有更好的方法,请留言。
正如 bastijn 所说,我认为将视图模型封装到视图模型中不是正确的做法。
Here is what I have in my controller.
Here is my View model.
Here is my View.
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.