ASP.NET MVC - 如何显示引用自身的模型的值

时间:2017-12-03 04:03:00

标签: asp.net-mvc

我有一个带有引用自身的ParentID的模型。

    public partial class Categories
    {
      public long CategoryID { get; set; }
      public string CategoryName { get; set; }
      public Nullable<int> ParentID { get; set; }
    }

在列表视图(索引视图)中,我希望能够显示ParentID的CategoryName(其中ParentID不为null)而不是ParentID。例如,我在下图中显示的是空白父母,因为我不知道如何去做。

Category

索引视图

    @foreach (var item in Model)
    {
        <tr id="row_@item.CategoryID">
            <td>@i</td>
            <td>
                @Html.DisplayFor(modelItem => item.CategoryName)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.ParentID)
            </td>

        </tr>
        i = i + 1;
    }

我如何实现这个

1 个答案:

答案 0 :(得分:0)

我已经复制了你的问题来解决它。

  
      
  1. 首先我创建了一个模型。
  2.   
public class Categories
{
    public long CategoryID { get; set; }
    public string CategoryName { get; set; }
    public Nullable<int> ParentID { get; set; }
}
  
      
  1. 具有索引操作方法的控制器,它将类别列表返回到索引视图。
  2.   

代码段

        public ActionResult Index()
        {

            List<Categories> li = new List<Models.Categories>()
            {

                new Categories { CategoryID = 0 , CategoryName = "Work" , ParentID = 1},

                new Categories { CategoryID = 0 , CategoryName = "Goods" , ParentID = 2},

                new Categories { CategoryID = 0 , CategoryName = "Service",ParentID = 1 },

                new Categories { CategoryID = 0 , CategoryName = "Buildings",ParentID = 1},
            };

            return View(li);
        }
  
      
  1. 接下来,我们创建了一个静态方法,可以在View上访问,此方法将ParentID作为输入。
  2.         

    通过父级后,它将获取类别名称,这将返回   查看。

代码段

namespace WebApplication1.Models
{
    public static class ManageCategories
    {
        public static string GetCategoriesbyParentID(int? ParentID)
        {
            string data = string.Empty;

            List<Categories> li = new List<Categories>()
            {
                new Categories { CategoryID = 0 , CategoryName = "Work" , ParentID = 1},
                new Categories { CategoryID = 0 , CategoryName = "Goods" , ParentID = 2},
                new Categories { CategoryID = 0 , CategoryName = "Service",ParentID = 1 },
                new Categories { CategoryID = 0 , CategoryName = "Buildings",ParentID = 1},
            };

            data = (from a in li
                    where a.ParentID == ParentID
                    select a.CategoryName).FirstOrDefault();

            return data;

        }
    }
}
  
      
  1. 查看(我们在其上调用静态方法GetCategoriesbyParentID并将ParentID传递给它。)
  2.   

代码

    @model List<WebApplication1.Models.Categories>

    @{ 
    Layout = null;
    }

    <link href="~/Content/bootstrap.css" rel="stylesheet" />

    @{int i = 1;}

    <table class="table">

        <tr>
            <td>CategoryID</td>
            <td>Bussiness Category Name</td>
            <td>Parent Bussiness Category</td>
        </tr>


        @foreach (var item in Model)
        {

            <tr id="row_@item.CategoryID">
                <td>@i</td>
                <td>
                    @Html.DisplayFor(modelItem => item.CategoryName)
                </td>

                <td> @WebApplication1.Models.ManageCategories.GetCategoriesbyParentID(item.ParentID)</td>

            </tr>
            i = i + 1;
        }
    </table>
</div>
  

输出: -

Output