类别和子类别 MVC2
我有一个大问题,至少对我来说是这样。 我有一个包含类别和无限子类别的表。 该表如下所示:
ID Parent_ID Name
1 null骑乘
2 1 gallopa
3 null图
4 2 trapper
并且有一个表包含归属于某个类别的项目。 该表如下所示:
ID cattegory_ID Name
1 4 fast
2 1 Slow
3 3 high
4 2 low
现在我想从数据库中检索它们并在我的 mvc2 应用程序中显示它们,如下所示: 第一个类别的字段集,以及之前字段集中的子类别的字段集。这些项目应在带有复选框的字段集中列出。 https://i.sstatic.net/lyMWD.png
我喜欢使用 @Html.CheckBoxfor
。
有人知道吗?自上周以来我一直在研究这个问题,但没有结果。 我试图递归地解决这个问题,但没有成功。 一个例子就太好了,非常感谢!
非常感谢您的回答! 一切正常!但是如何用这个模型做一个 Httppost 呢?以及如何获取每个复选框的“选中”或“未选中”状态?
这是我的开始:
[HttpPost]
public ActionResult CreateNewHorse(NewHorseModel collection)
{
collection.Cattegories.Count(); <------------is always null! Why?
}
I have a big problem, at least for me.
I have a Table with categories and unlimited subcategories.
The table looks like this:
ID Parent_ID Name
1 null riding
2 1 gallopa
3 null figure
4 2 trapper
And there is a table containing items wich are attributed to a category.
The table looks like this:
ID cattegory_ID Name
1 4 fast
2 1 slow
3 3 high
4 2 low
Now I want to retrieve them from the database and show them in my mvc2 application like this:
A Fieldset for the first category, and one for the subcategory in the fieldset before. The items should be listed in the fieldset with checkboxes.
https://i.sstatic.net/lyMWD.png
I like to work with @Html.CheckBoxfor
.
Has any one got any idea? I'm working on this problem since last week without a result.
I tried to solve the problem recursively but it did not work.
An example would be beautiful, thanks a lot!
Thanks a lot for your answer!
Everything works fine! But how to do a Httppost with this model? And how to get the Status Checked or not Checked of every checkbox?
here is my start:
[HttpPost]
public ActionResult CreateNewHorse(NewHorseModel collection)
{
collection.Cattegories.Count(); <------------is always null! Why?
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以创建一个将 Category 类作为模型的 PartialView,如下所示:
Category.cshtml
Choice.cshtml
在主视图中,您只需渲染基于类别的部分视图:
现在您只需将根类别传递到您的视图中
这应该会给您一个包含类别和/或子类别的递归视图。
更新
视图模型/模型可能如下所示:
Category.cs
Choice.cs
我已将顶部的代码更新为好吧,反映这个模型
希望这有帮助!
You could create a PartialView that has the Category-class as a Model, something like this:
Category.cshtml
Choice.cshtml
in your main view, you'd simply render the partialview based on the categories:
Now you need to pass only the root categories into your view
This should give you a recursive view with categories and/or subcategories.
UPDATE
The viewmodel/model could look like this:
Category.cs
Choice.cs
I've updated the code in the top as well, to reflect this Model
Hope this helps!