ghost...
ghost...

Reputation: 1001

MVC in asp.net view not coming

My model goes like this

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();
        }
    }
}

}

my controller is

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);
        }
    }
}

the view generated is

<%@ 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>

the problem is that

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

is not giving me any output...i check dataval value by debuging...its holding the value......... error is

The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[CustomerBook.Models.CustomerModels]', but this dictionary requires a model item of type 'CustomerBook.Models.CustomerModels'.

plz tell me where m i wrong

Upvotes: 0

Views: 139

Answers (3)

ghost...
ghost...

Reputation: 1001

Putting IEnumerable in

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

Like this:

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

solved the problem..!!

Upvotes: 0

Alexandros B
Alexandros B

Reputation: 1891

Method getData() returns List<CustomerModels> while the view expects CustomerBook.Models.CustomerModels

either change the view to

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

or have getData() return CustomerModels

Upvotes: 1

Darin Dimitrov
Darin Dimitrov

Reputation: 1038710

The error message is pretty obvious. You are passing a List<CustomerModels> to the view from the controller action and your view is strongly typed only to a single CustomerModels.

So you could change the model type for your view:

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

and then be able to loop through the Model and display the results:

<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>

Upvotes: 1

Related Questions