类别和子类别 MVC2

发布于 2024-10-27 08:46:06 字数 929 浏览 0 评论 0原文

我有一个大问题,至少对我来说是这样。 我有一个包含类别和无限子类别的表。 该表如下所示:

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 技术交流群。

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

发布评论

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

评论(1

夕嗳→ 2024-11-03 08:46:06

您可以创建一个将 Category 类作为模型的 PartialView,如下所示:

Category.cshtml

@model Category

<fieldset>
    <legend>@Model.Name</legend>
    @foreach (var item in Model.Choices)
    {
        Html.RenderPartial("Choice", item);
    }

    @foreach(var item in Model.Subcategories)
    {
        Html.RenderPartial("Category", item);
    }
</fieldset>

Choice.cshtml

@model StackOverflow_Tester.Models.Choice

<p>@Html.LabelFor(m => m.Selected) @Html.CheckBoxFor(m => m.Selected)</p>

在主视图中,您只需渲染基于类别的部分视图:

@foreach (var item in Model)
{
    Html.RenderPartial("Category", item);
}

现在您只需将根类别传递到您的视图中

这应该会给您一个包含类别和/或子类别的递归视图。

更新

视图模型/模型可能如下所示:

Category.cs

public class Category
{
    public int Id { get; set; }
    public string Name { get; set; }
    public List<Category> Subcategories { get; set; }
    public List<Choice> Choices { get; set; }
    public Category Parent { get; set; }
}

Choice.cs

public class Choice
{
    public int Id { get; set; }
    public string Name { get; set; }
    public bool Selected { get; set; }
}

我已将顶部的代码更新为好吧,反映这个模型

希望这有帮助!

You could create a PartialView that has the Category-class as a Model, something like this:

Category.cshtml

@model Category

<fieldset>
    <legend>@Model.Name</legend>
    @foreach (var item in Model.Choices)
    {
        Html.RenderPartial("Choice", item);
    }

    @foreach(var item in Model.Subcategories)
    {
        Html.RenderPartial("Category", item);
    }
</fieldset>

Choice.cshtml

@model StackOverflow_Tester.Models.Choice

<p>@Html.LabelFor(m => m.Selected) @Html.CheckBoxFor(m => m.Selected)</p>

in your main view, you'd simply render the partialview based on the categories:

@foreach (var item in Model)
{
    Html.RenderPartial("Category", item);
}

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

public class Category
{
    public int Id { get; set; }
    public string Name { get; set; }
    public List<Category> Subcategories { get; set; }
    public List<Choice> Choices { get; set; }
    public Category Parent { get; set; }
}

Choice.cs

public class Choice
{
    public int Id { get; set; }
    public string Name { get; set; }
    public bool Selected { get; set; }
}

I've updated the code in the top as well, to reflect this Model

Hope this helps!

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