我在我的视图上看到这个奇怪的文本:System.Collections.Generic.HashSet`1 [automasis.Models.ElencoProvince] System.Collections.Generic.HashSet

时间:2015-10-19 16:48:54

标签: c# entity-framework visual-studio model-view-controller razor

问候我觉得这是一个非常奇怪的消息。

  

(System.Collections.Generic.HashSet`1 [automasis.Models.ElencoProvince] System.Collections.Generic.HashSet`1 [automasis.Models.ElencoProvince])

我有一个数据库,我使用entinty第一个代码建立连接。这是我使用的模型视图:

public IEnumerable<ElencoOmonimi> elencoomonimi { get; set; }
public IEnumerable<ElencoProvince> elencoprovince { get; set; }
public IEnumerable<ElencoImmobiliPerDiritti_E_Quote>      elencoimmobiliperditti { get; set; }
public IEnumerable<ElencoComuni> elencocomuni { get; set; }
public IEnumerable<ElencoIntestati> elencointestati { get; set; }

和控制器:

public ActionResult Index(string searchString, int? id, int? courseID, int? idcom, int? elencoimo)
{
    var viewmodel = new mainview();

    viewmodel.elencoomonimi = db.ElencoOmonimis
        .Where(s => s.Nome.Contains(searchString) || searchString == null || searchString == "")
        .Include(s => s.ElencoProvinces.Select(t => t.ElencoImmobiliPerDiritti_E_Quote))
        .Include(s => s.ElencoProvinces.Select(t => t.ElencoComunis))
        .Include(s => s.ElencoProvinces.Select(t => t.ElencoImmobiliPerDiritti_E_Quote.Select(r => r.ElencoIntestatis)))

        ////.Include(i => i.ElencoOmonimi)
        ////.Include(i => i.ElencoImmobiliPerDiritti_E_Quote.Select(t => t.ElencoIntestatis))
        ////.Include(i => i.ElencoComunis)
        .OrderBy(i => i.Id);

    if (id != null)
    {
        ViewBag.elencoomonimiID = id.Value;
        viewmodel.elencoprovince = viewmodel.elencoomonimi.Where(
            i => i.Id == id.Value).Single().ElencoProvinces;
    }
    if (idcom != null)
    {
        ViewBag.ElencoImmobiliperID = idcom.Value;
        viewmodel.elencointestati = viewmodel.elencoimmobiliperditti.Where(
            x => x.Id == idcom.Value).Single().ElencoIntestatis;
    }
    //if(elencoimo != null)
    //    ViewBag.elencoimoobiliID = id.Value

    return View(viewmodel);
}

我的观点:

@model automasystem.Models.mainview

@{
    ViewBag.Title = "Index";
}

Dashboard



@using (Html.BeginForm())
{

    @Html.TextBox("SearchString") <br />
    <input type="submit" value="Filter" />
}

<table class="table">
    <tr>
        <th>
            Nome
        </th>
        <th>
            Cognome
        </th>
        <th>
            Data Di Nascita
        </th>
        <th>
            Codice Fiscale
        </th>
        <th></th>
    </tr>

    @foreach (var item in Model.elencoomonimi)
    {
        string selectedRow = "";
        if (item.Id == ViewBag.elencoomonimiID)
        {
            selectedRow = "success";
        }

        <tr class="@selectedRow">
            <td>
                @Html.DisplayFor(modelItem => item.CognomeCercato)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.NomeCercato)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Cognome)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.DataDiNascita)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.CodiceFiscale)
            </td>
            @*<td>
                    @Html.DisplayFor(modelItem => item.Provincia)
                </td>

                <td>
                    @Html.DisplayFor(modelItem => item.Fabbricati)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Terreni)
                </td>*@
            @if (item.ElencoProvinces != null)
            {
                @item.ElencoProvinces
            }
            <td>
                @Html.ActionLink("Select", "Index", new { id = item.Id }) |
                @Html.ActionLink("Edit", "Edit", new { id = item.Id }) |
                @Html.ActionLink("Details", "Details", new { id = item.Id }) |
                @Html.ActionLink("Delete", "Delete", new { id = item.Id })
            </td>
        </tr>
    }

我不明白为什么我会为数据库中的每条记录得到这个:

  

System.Collections.Generic.HashSet`1 [automasis.Models.ElencoProvince] System.Collections.Generic.HashSet`1 [automasis.Models.ElencoProvince]

1 个答案:

答案 0 :(得分:4)

您在视图中有这个(嵌入<tr>但不在<td>内,因此它实际显示在表格上方!)

@if (item.ElencoProvinces != null)
{
    @item.ElencoProvinces
}

那将尝试输出该对象的字符串表示item.ElencoProvinces.ToString()的值显然是

  

System.Collections.Generic.HashSet`1 [automasis.Models.ElencoProvince]

如果您不想在视图中ElencoProvinces,请删除这几行。如果你想要它然后遍历hashset,或从其中的特定项中提取特定信息,并在适当的HTML中格式化。