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

エラーメッセージは非常に明白です。List<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 に答える