【问题标题】:Display Country Entity inside the Index在索引中显示国家实体
【发布时间】:2013-08-21 18:49:13
【问题描述】:

我有一个名为 Fixture 的实体,基本上我想在页面上显示一些灯具。

我的模型如下:-

    public class Fixture
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int FixtureId { get; set; }

    [Display(Name = "Fixture Date")]
    [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
    public DateTime? FixtureDate { get; set; }

    public DateTime? FixtureTime { get; set; }

    public int? CityId { get; set; }
    public City City { get; set; }

    public int CountryIdA { get; set; }
    public int CountryIdB { get; set; }

    public int? ScoreA { get; set; }
    public int? ScoreB { get; set; }

    public Round Round { get; set; }
    public int? RoundId { get; set; }

    public List<Scorer> Scorers { get; set; }
}

我的 ViewModel 如下:-

    public class FixtureViewModel
{
    public Fixture Fixture { get; set; }
    public IEnumerable<Fixture> FixtureList { get; set; }

    public IEnumerable<GroupCountry> GroupCountryList { get; set; }

    public IEnumerable<Group> GroupList { get; set; }

    public string CountryNameA { get; set; }
    public string CountryNameB { get; set; }
}

我在这个索引页面中显示它们:-

<table>
<tr>
    <th>
        @Html.DisplayName("Date")
    </th>
    <th>
        @Html.DisplayName("Country A")
    </th>
    <th>
        @Html.DisplayName("Country A")
    </th>
    <th>
        @Html.DisplayName("Score A")
    </th>
    <th>
        @Html.DisplayName("Score B")
    </th>
    <th>
        @Html.DisplayName("Round")
    </th>
    <th></th>
</tr>

@foreach (Fixture item in Model.FixtureList)
{
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.FixtureDate)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.CountryIdA)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.CountryIdB)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.ScoreA)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.ScoreB)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Round.RoundName)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new {id = item.FixtureId}) |
            @Html.ActionLink("Details", "Details", new {id = item.FixtureId}) |
            @Html.ActionLink("Delete", "Delete", new {id = item.FixtureId})
        </td>
    </tr>
}

</table>

现在在索引页面中,我希望显示如下内容:-

            <td>
            @Html.DisplayFor(modelItem => item.CountryIdA.Country.CountryName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.CountryIdB.Country.CountryName)
        </td>

我怎样才能做到这一点?

我在模型中添加了导航项 Country,但仍然无法显示正确的 CountryName。

感谢您的帮助和时间

【问题讨论】:

    标签: asp.net-mvc c#-4.0 razor ef-code-first


    【解决方案1】:

    CountryIdACountryIdB 只是整数。如果你想说...

    用 CountryIdA/B 找到一个灯具,然后得到它的 Country,然后是 CountryName

    那么这不适合放在视图中。您的视图不应处理任何数据访问,这是您的控制器/模型的工作。我建议在您的视图模型中添加两​​个 Country 属性,一个用于 IdA,一个用于 IdB。然后,您可以在控制器操作中填充这些内容,并在您的视图中访问它们。

    您的控制器可能看起来像...

    ViewModel.CountryA = Country.Where(x => x.CountryID = ViewModel.CountryIdA)

    【讨论】:

    • 嗨,Elliot,当我尝试这样做时,由于我首先使用代码,它在数据库中添加了 2 个新字段,即 Country_CountryIdA 和 Country_CountryIdB。我不希望在数据库中有这个。这可能吗?
    • 将它们添加到您的视图模型,而不是您的表模型。这就是视图模型的目的。
    • 我已经在我的 ViewModel 中添加了它们,但是我不知道如何填充它们。但是我不知道如何填充它们。我知道我必须遍历索引控制器操作中的列表,但我不知道如何填充它们并在视图中显示它们
    • 那么您必须有权访问您的国家/地区存储库。而且您已经拥有 Country IdA 和 IdB 对吗?然后,您需要在控制器中做的所有事情(如上所示)就是调用您的存储库 WHERE countryid 等于 IdA 或 IdB。
    • 是的,我明白你的意思,但是因为我有一个来自 ViewModel 的 Fixtures 列表:- viewModel.FixtureList = unitOfWork.FixturesRepository.Get(orderBy: d=> d.OrderBy(x= >x.FixtureDate).ThenBy(x=>x.CountryIdA), includeProperties: "Round"); ,如何将 ViewModel.CountryA 和 ViewModel.CountryB “附加”到此列表?
    【解决方案2】:

    您可以通过在同一命名空间中创建部分类 Fixture 来扩展模型中的 Fixture,创建两个自定义属性 CountryIdA/B 来查找当前 ID。

    【讨论】:

      猜你喜欢
      • 2014-12-19
      • 1970-01-01
      • 1970-01-01
      • 2022-11-04
      • 2019-12-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多