0

我的模型是这样的

namespace CustomerBook.Models
{
    public class CustomerModels
    {
        public string fname { set; get; }
        public string lname { set; get; }
        public string city { set; get; }
        public List<CustomerModels> lst;

    public List<CustomerModels> getData()
    {
        string path = @"somepath\MvcApplication13\\Book1.xlsx";
        string excelConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0;";
        OleDbConnection excelConnection = new OleDbConnection(excelConnectionString);
        OleDbCommand ocmd = new OleDbCommand("select * from [Sheet1$]", excelConnection);
        excelConnection.Open();
        OleDbDataReader odr = ocmd.ExecuteReader();

        lst = new List<CustomerModels>();
        CustomerModels modl;
        while (odr.Read())
        {
            modl = new CustomerModels();
            modl.fname =valid(odr, 0);
            modl.lname = valid(odr, 1);
            modl.city = valid(odr, 2);
            lst.Add(modl);
        }
        excelConnection.Close();
        return lst;
    }
    protected string valid(OleDbDataReader myreader, int stval)
    {
        //if any columns are found null then they are replaced by zero
        object val = myreader[stval];
        if (object.ReferenceEquals(val, DBNull.Value))
        {
            return Convert.ToString(0);
        }
        else
        {
            return val.ToString();
        }
    }
}

}

我的控制器是

using System.Linq;
using System.Web;
using System.Web.Mvc;
using CustomerBook.Models;
namespace CustomerBook.Controllers
{
    public class LoadCustomerAndDisplayController : Controller
    {
        //
        // GET: /LoadCustomerAndDisplay/
        public ActionResult Index()
        {
            CustomerModels objCustomer = new CustomerModels();
            var dataval = objCustomer.getData();
            return View(dataval);
        }
    }
}

生成的视图是

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<CustomerBook.Models.CustomerModels>" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Index</title>
</head>
<body>
    <div>
        hhiiiiiiiiiiiiii<br />
        <%=Model.fname %>
        <%=Model.lname %>
        <%=Model.city %>
    </div>
</body>
</html>

问题是

<%=Model.fname %>
            <%=Model.lname %>
            <%=Model.city %>

没有给我任何输出...我通过调试检查 dataval 值...它持有该值.........错误是

传入字典的模型项的类型为“System.Collections.Generic.List`1[CustomerBook.Models.CustomerModels]”,但此字典需要“CustomerBook.Models.CustomerModels”类型的模型项。

请告诉我哪里错了

4

3 回答 3

1

错误信息非常明显。您正在将 aList<CustomerModels>从控制器操作传递给视图,并且您的视图仅被强类型化为单个CustomerModels.

因此,您可以更改视图的模型类型:

<%@ Page 
    Language="C#" 
    Inherits="System.Web.Mvc.ViewPage<List<CustomerBook.Models.CustomerModels>>" 
%>

然后能够遍历模型并显示结果:

<table>
    <thead>
        <tr>
            <th>First name</th>
            <th>Last name</th>
            <th>City</th>
        </tr>
    </thead>
    <tbody>
        <% foreach (var item in Model) { %>
            <tr>
                <td><%: item.fname %></td>
                <td><%: item.lname %></td>
                <td><%: item.city %></td>
            </tr>
        <% } %>
    </tbody>
</table>
于 2012-07-19T10:27:05.277 回答
1

方法在视图期望时getData()返回List<CustomerModels>CustomerBook.Models.CustomerModels

要么将视图更改为

System.Web.Mvc.ViewPage<System.Collections.Generic.List<CustomerBook.Models.CustomerModels>>

或有getData()回报CustomerModels

于 2012-07-19T10:28:07.850 回答
0

投入IEnumerable_

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<CustomerBook.Models.CustomerModels>" %> 

像这样:

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<IEnumerable<CustomerBook.Models.CustomerModels>>" %>

解决了问题..!!

于 2012-07-20T06:29:55.540 回答