Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 在索引中显示国家/地区实体_Asp.net Mvc_C# 4.0_Razor_Ef Code First - Fatal编程技术网

Asp.net mvc 在索引中显示国家/地区实体

Asp.net mvc 在索引中显示国家/地区实体,asp.net-mvc,c#-4.0,razor,ef-code-first,Asp.net Mvc,C# 4.0,Razor,Ef Code First,我有一个名为Fixture的实体,基本上我想在页面上显示一些Fixture 我的模型如下:- public class Fixture { [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int FixtureId { get; set; } [Display(Name = "Fixture Date")] [DisplayFormat(DataFormatSt

我有一个名为Fixture的实体,基本上我想在页面上显示一些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; }
}
    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; }
}
公共类设备
{
[关键]
[数据库生成(DatabaseGeneratedOption.Identity)]
公共int FixtureId{get;set;}
[显示(Name=“夹具日期”)]
[DisplayFormat(DataFormatString=“{0:dd/MM/yyyy}”,ApplyFormatInEditMode=true)]
公共日期时间?FixtureDate{get;set;}
公共日期时间?固定时间{get;set;}
公共int?CityId{get;set;}
公共城市城市{get;set;}
公共int CountryIdA{get;set;}
public int CountryIdB{get;set;}
公共int?ScoreA{get;set;}
公共int?ScoreB{get;set;}
公共轮{get;set;}
public int?RoundId{get;set;}
公共列表记分器{get;set;}
}
我的ViewModel如下所示:-

    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; }
}
    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; }
}
公共类FixtureViewModel
{
公共设备{get;set;}
公共IEnumerable固定列表{get;set;}
公共IEnumerable GroupCountryList{get;set;}
公共IEnumerable组列表{get;set;}
公共字符串CountryNameA{get;set;}
公共字符串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>

@Html.DisplayName(“日期”)
@Html.DisplayName(“国家A”)
@Html.DisplayName(“国家A”)
@Html.DisplayName(“分数A”)
@Html.DisplayName(“分数B”)
@Html.DisplayName(“圆形”)
@foreach(Model.FixtureList中的夹具项目)
{
@DisplayFor(modelItem=>item.FixtureDate)
@DisplayFor(modelItem=>item.CountryIdA)
@DisplayFor(modeleItem=>item.CountryIdB)
@DisplayFor(modeleItem=>item.ScoreA)
@DisplayFor(modeleItem=>item.ScoreB)
@DisplayFor(modelItem=>item.Round.RoundName)
@ActionLink(“编辑”,“编辑”,新的{id=item.FixtureId})|
@ActionLink(“详细信息”,“详细信息”,新的{id=item.FixtureId})|
@ActionLink(“Delete”,“Delete”,new{id=item.FixtureId})
}
现在在索引页面中,我希望显示如下内容:-

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

@DisplayFor(modelItem=>item.CountryIdA.Country.CountryName)
@DisplayFor(modeleItem=>item.CountryIdB.Country.CountryName)
我怎样才能做到这一点

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


谢谢你的帮助和时间

CountryIdA
CountryIdB
都是整数。如果你想说

使用CountryIdA/B查找固定装置,然后获取其所在国家,然后是CountryName

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

您的控制器可能看起来像


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

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

嗨,Elliot,当我尝试这样做时,因为我首先使用代码,它在数据库中添加了两个新字段:Country_CountryIdA和Country_CountryIdB。我不希望在db中包含此内容。可能吗?将它们添加到视图模型中,而不是表模型中。这就是视图模型的目的。我已经在我的视图模型中添加了它们,但是我不知道如何填充它们。但是,我不知道如何填充它们。我知道我必须在Index Controller操作中遍历列表,但我不知道如何填充它们并在viewWell中显示它们。您必须有权访问您的国家/地区存储库。你已经有了IdA和IdB,对吗?然后,您需要在控制器中执行的所有操作(如上所示)就是调用存储库,其中countryid等于IdA或IdB。是的,我理解您的意思,但是因为我有一个来自ViewModel的装置列表:-ViewModel.FixtureList=unitOfWork.FixturesRepository.Get(orderBy:d=>d.orderBy(x=>x.FixtureDate)。然后(x=>x.CountryIdA),includeProperty:“Round”);如何将ViewModel.CountryA和ViewModel.CountryB“附加”到此列表中?