MVC主详细信息视图以及相关记录

MVC主详细信息视图以及相关记录

本文介绍了.net MVC主详细信息视图以及相关记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我要制作一个主从视图.

I am looking to make a Master Detail View.

我有两个表tbFamily和tbFamilyMember.

I have two tables tbFamily and tbFamilyMember.

我目前能够查看家庭详细信息"并列出相关的家庭成员,但是,我想根据其角色来区分家庭成员.即,我想显示一个父母/监护人"列表,然后显示一个按出生日期排序的孩子的列表.

I am currently able to view the Family Details and list the associated family members, however, I want to seperate the Family Members based on their Role. i.e. I want to display a list of the Parents/Guardians and then a list of the children ordered by Date of Birth.

我的模型如下:

public class tbFamily
{
    public int tbFamilyID { get; set; }
    public string Surname { get; set; }
    public string Address1 { get; set; }
    public string Address2 { get; set; }
    public string Address3 { get; set; }
    public string Town { get; set; }
    public string County { get; set; }
    public string Postcode { get; set; }
    public string Country { get; set; }
    public string Telephone { get; set; }
    public string Mobile { get; set; }
    public string Email { get; set; }

    public virtual ICollection<tbFamilyMember> tbFamilyMember { get; set; }
    public virtual ICollection<tbFamilyData> tbFamilyData { get; set; }
}

public class tbFamilyMember
{
    public int tbFamilyMemberID { get; set; }
    public int tbFamilyID { get; set; }
    public int Role { get; set; }
    public string Firstname { get; set; }
    public string Surname { get; set; }
    public char Gender { get; set; }

    [DisplayFormat(DataFormatString = "{0:d}", ApplyFormatInEditMode = true)]
    public DateTime DateOfBirth { get; set; }

    public virtual tbFamily tbFamily { get; set; }
}

我的控制器当前如下所示:

My Controller currently looks as follows:

    public ActionResult Details(int id = 0)
    {
        tbFamily tbfamily = db.tbFamily.Find(id);
        if (tbfamily == null)
        {
            return HttpNotFound();
        }
        return View(tbfamily);
    }

其关联的视图

@Html.DisplayFor(model => model.Surname)
@Html.DisplayFor(model => model.Address1)
@Html.DisplayFor(model => model.Address2)
@Html.DisplayFor(model => model.Address3)
@Html.DisplayFor(model => model.Town)
@Html.DisplayFor(model => model.County)
@Html.DisplayFor(model => model.Postcode)
@Html.DisplayFor(model => model.Country)
@Html.DisplayFor(model => model.Telephone)
@Html.DisplayFor(model => model.Mobile)
@Html.DisplayFor(model => model.Email)

@foreach (var item in Model.tbFamilyMember)
{
    @Html.DisplayFor(modelItem => item.Firstname)
    @Html.DisplayFor(modelItem => item.Surname)
    @Html.DisplayFor(modelItem => item.Role)
}

要得到孩子们:

public ActionResult GetChildren(int id)
{
    var tbFamilyData = from f in db.tbFamilyMember
                       where f.tbFamilyID.Equals(id)
                       where f.Role.Equals(3)
                       select f;
        return View(tbFamilyData);
}

其关联的视图:

@foreach (var item in Model.tbFamilyData)
{
    @Html.DisplayFor(modelItem => item.tbFamilyMember.Firstname)
    @Html.DisplayFor(modelItem => item.tbFamilyMember.Surname)
    @Html.DisplayFor(modelItem => item.tbFamilyMember.Role)
}

只是不确定如何使两者一起工作!

Just not sure how to get the two working together!

我创建了一个单独的模型tbFamilyData

I have created a seperate Model tbFamilyData

public class tbFamilyData
{
    public virtual tbFamily tbFamily { get; set; }
    public virtual tbFamilyMember tbFamilyMember { get; set; }
}

我现在收到错误tbFamilyData尚未定义键.

I am now getting the error tbFamilyData has no key defined.

希望有人可以提供帮助.

Hoping someone can help.

推荐答案

我建议以下两个选项之一:

I would suggest one of two options:

1)创建可以更好地表示您的视图的视图模型,并将其填充到控制器中(我的首选解决方案).

1) create viewmodels that better represent your views, and fill them in the controller (my preferred solution).

public class FamilyViewModel
{
    public string Surname { get; set; }
    ... // other props

    public ICollection<FamilyMemberViewModel> Parents { get; set; }
    public ICollection<FamilyMemberViewModel> Children { get; set; }
}

2)在您的视图中使用Linq方法来过滤要显示的数据

2) use Linq methods in your view to filter the data to display

<h1>Parents</h1>
@foreach (var item in Model.tbFamilyMember.Where(x => x.Role == Roles.Parent))
{
    ...
}

<h1>Children</h1>
@foreach (var item in Model.tbFamilyMember.Where(x => x.Role == Roles.Child)
                                          .OrderBy(x => x.DateOfBirth))
{
    ...
}

这篇关于.net MVC主详细信息视图以及相关记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 18:11