类别和子类别MVC2 [英] Categories and Subcategories MVC2

查看:115
本文介绍了类别和子类别MVC2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个很大的问题,至少对我来说。
我有类别和子类别无限表。
该表是这样的:

ID PARENT_ID名称

1空骑

2 1 gallopa

3空图

4 2捕兽

和存在包含项目表至极都归于一类。
该表是这样的:

ID cattegory_ID名称

1 4快

2 1慢

3高3

4 2低

现在我想从数据库中检索他们,并表明他们在这样我的MVC2应用程序:
一种用于第一类字段集,以及一个用于在之前的字段集的子类别。这些项目应与复选框的字段集上市。
http://i.stack.imgur.com/lyMWD.png

我喜欢 @ Html.CheckBoxfor 工作

有没有人有任何想法?我工作的这个问题,因为上周没有结果。
我试图递归地解决这个问题,但没有奏效。
一个例子是美丽的,非常感谢!

非常感谢您的回答!
一切正常!但如何做这个模型Httppost?而如何让状态经过或未经过每一个复选框的?

这是我的启动:

  [HttpPost]
    公众的ActionResult CreateNewHorse(NewHorseModel集合)
    {
      collection.Cattegories.Count(); < ------------总是空!为什么?
    }


解决方案

您可以创建一个具有范畴类为模型的PartialView,是这样的:

Category.cshtml

  @model类别<&字段集GT;
    <传奇> @ Model.Name< /传说>
    @foreach(在Model.Choices VAR项)
    {
        Html.RenderPartial(选择,项目);
    }    @foreach(在Model.Subcategories VAR项)
    {
        Html.RenderPartial(类别,项目);
    }
< /字段集>

Choice.cshtml

  @model StackOverflow_Tester.Models.Choice&所述p为H.; @ Html.LabelFor(M => m.Selected)@ Html.CheckBoxFor(M => m.Selected)所述; / P>

在主视图,你只需呈现基于类别partialview:

  @foreach(以型号VAR项)
{
    Html.RenderPartial(类别,项目);
}

现在,你只需要根类别传递到您的看法

这应该给你类别和/或子类别的递归视图。

更新

视图模型/模型可以是这样的:

Category.cs

 公共类分类
{
    公众诠释标识{搞定;组; }
    公共字符串名称{;组; }
    公开名单<类别及GT;小类{搞定;组; }
    公开名单<选择与GT;选择{搞定;组; }
    公共范畴家长{搞定;组; }
}

Choice.cs

 公共类选择
{
    公众诠释标识{搞定;组; }
    公共字符串名称{;组; }
    公共BOOL选择{搞定;组; }
}

我已经更新在顶部code为好,以反映该型号

希望这有助于!

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. http://i.stack.imgur.com/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?
    }

解决方案

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!

这篇关于类别和子类别MVC2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆