1

我的控制器有问题。我的表中的数据未显示在我的强类型视图中。但是当我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>
4

2 回答 2

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);
}
于 2013-06-11T13:22:21.260 回答
0
 foreach (var item in Model)
    {
         @Html.LabelForModel(item.CanaClie0012.Client00130012)
    }
于 2013-06-11T17:13:33.237 回答