【问题标题】:Data not showing in the strongly typed view数据未显示在强类型视图中
【发布时间】:2013-09-28 23:00:21
【问题描述】:

我的控制器有问题。我的表中的数据未显示在我的强类型视图中。但是当我return View(CanaClie0012.toList());我可以完美地看到我的数据。我没有收到任何错误。谁能告诉我我做错了什么。

我的表格模型:

public partial class CanaClie0012
{
    public string Client00130012 { get; set; }
    public string F1Pais00200012 { get; set; }
    public string F1Cana02530012 { get; set; }
    public string Direcc0012 { get; set; }
    public Nullable<System.DateTime> TmStmp0012 { get; set; }
}

public partial class Clientes0013
{
    public string Client0013 { get; set; }
    public string Nombre0013 { get; set; }
    public string F1Pais00200013 { get; set; }
}

我的自定义模型组合这两个表是:

public class ClientModel
{
  public CanaClie0012 CanaClie0012 { get; set; }
  public Clientes0013 Clientes0013 { get; set; }
}

我的控制者:

public ViewResult Index()
{
    List<ClientModel> lm = new List<ClientModel>();
    ClientModel vm = new ClientModel();
    lm.Add(vm);

    return View(lm);
}

我的观点:

@model IEnumerable<ContactManager.Models.ClientModel>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table>
    <tr>
        <th>
            Client00130012
        </th>
        <th>
            F1Pais00200012
        </th>
        <th>
            F1Cana02530012
        </th>
        <th>
            Direcc0012
        </th>
        <th>
            TmStmp0012
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.CanaClie0012.Client00130012)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.CanaClie0012.F1Pais00200012)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.CanaClie0012.F1Cana02530012)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.CanaClie0012.Direcc0012)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.CanaClie0012.TmStmp0012)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
            @Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
            @Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
        </td>
    </tr>
}

</table>

【问题讨论】:

  • DisplayFor 是什么?为什么不@item.CanaClie0012.Client00130012?
  • 尝试使用@Html.LabelFor 而不是DisplayFor
  • 嗨@chamara,当我使用 Html.LabelFor 它只显示我的表的字段名。
  • @rcadaoas 试试我的答案

标签: asp.net-mvc asp.net-mvc-3 razor


【解决方案1】:

在您的控制器中,您所做的只是实例化一个新的 ClientModel 对象并将其添加到列表中。这真的是您的索引方法中的内容吗?如果是这样,属性将为空,问题在于您的控制器而不是您的视图。如果没有,您能否发布您的索引方法的实际作用?此外,在您的 ClientModel 中,属性是 CanaClie0012 那么 CanaClie0012.toList() 是如何发挥作用的呢?

更新

我不确定 CanaClie0012 和 Clientes0013 是如何相关的,尽管我假设您将它们组合在 ClientModel 中。举个例子,让我们通过在 CanaClie0012 中添加一个 id 字段和在 Clientes0013 中添加一个相应字段来修改它们,以便我们可以加入这两个类。同样,我这样做只是为了展示控制器的外观。所以现在我们有了以下内容。

public partial class CanaClie0012
{
    public string Id {get; set; }
    public string Client00130012 { get; set; }
    public string F1Pais00200012 { get; set; }
    public string F1Cana02530012 { get; set; }
    public string Direcc0012 { get; set; }
    public Nullable<System.DateTime> TmStmp0012 { get; set; }
}

public partial class Clientes0013
{
    public string CanaClie0012Id {get; set; }
    public string Client0013 { get; set; }
    public string Nombre0013 { get; set; }
    public string F1Pais00200013 { get; set; }
}

那么你的控制器可能看起来像。

public ViewResult Index()
{
    using (var context = new MMProdatEntities()) 
    { 
        var lm = from cc in context.CanaClie0012
        join c in context.Clientes0013 on cc.Id equals c.CanaClie0012Id
        select new ClientModel() {
            CanaClie0012 = new CanaClie0012() {
                Client00130012 = cc.Client00130012,
                F1Pais00200012 = cc.F1Pais00200012,
                F1Cana02530012 = cc.F1Cana02530012,
                Direcc0012 = cc.Direcc0012
            },
            Clientes0013 = new Clientes0013() {
                Client0013 = c.Client0013,
                Nombre0013 = c.Nombre0013,
                F1Pais00200013 = c.F1Pais00200013
            }
        };
        return View(lm);
    }

}

哎呀,您甚至可以执行以下操作来确认问题与视图无关

public ViewResult Index()
{
    List<ClientModel> lm = new List<ClientModel>();
    ClientModel vm = new ClientModel() 
    {
        CanaClie0012 = new CanaClie0012() 
        {
            Client00130012 = "Client00130012 Data",
            F1Pais00200012 = "F1Pais00200012 data",
            F1Cana02530012 = "F1Cana02530012 data"
        },
        Clientes0013 = new Clientes0013() 
        {
            Client0013 = "Client0013 data",
            Nombre0013 = "Nombre0013 data",
            F1Pais00200013 = "F1Pais00200013 data"
        }
    };
    lm.Add(vm);

    return View(lm);
}

【讨论】:

  • 你能告诉我我的控制器应该是什么样子吗?我很难将数据添加到我实例化的新 ClientModel。我所说的“CanaClie0012.toList()”的意思是,当我只使用我的 CanaClie0012 模型而不是自定义组合视图模型时,我可以查看表中的所有数据。当然,当我返回 CanaClie0012.toList() 时,我也改变了我的视图结构。
  • 我想我不明白您的数据访问层是如何工作的。您必须有一些逻辑可以返回列表/可编号/可查询/或其他东西,而不是简单地实例化一个新对象。您如何填充您的 canaClie0012 列表?
  • 这是我的 DbContext MMProdatEntities db = new MMProdatEntities();
  • 然后使用你的上下文来检索你需要的任何数据using (var context = new MMProdatEntities()) { /* some select statement */ }
  • 根据 cmets 更新答案
【解决方案2】:
 foreach (var item in Model)
    {
         @Html.LabelForModel(item.CanaClie0012.Client00130012)
    }

【讨论】:

  • 显示错误:对象引用未设置为对象的实例。
  • 这可能是您从数据库中获取的值(NULL 值)的情况。首先尝试在您的 HTML 表中显示单个列并确保它有效。
猜你喜欢
  • 2012-09-30
  • 2011-09-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多