Click here to Skip to main content
15,908,172 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello
I am new to asp.net mvc2 ,but i have experience in ASP.net c#.Now using the tutorial posted on Codeproject.
now am getting an error when passing values from controller to view

HTML
Server Error in '/' Application.

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


Controller Code
----------------
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MyFirstHelloWorld.Models;

namespace MyFirstHelloWorld.Controllers
{
    public class CustomerController : Controller
    {
        //
        // GET: /Customer/

        public ActionResult Index()
        {
            return View();
        }

       public ViewResult DisplayCustomer()
       {
           Customer cs = new Customer();
           cs.Code = "12";
           cs.Name = "Salil Sasidharan";
           cs.Amount = 23500;

           return View("DisplayCustomer", cs);

           
       }

    }
}


Model Code
-----------
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MyFirstHelloWorld.Models
{
    public class Customer
    {
        private string _Code;
        private string _Name;
        private double _Amount;

        public string Code
        {
            set
            {
                _Code = value;
            }
            get
            {
                return _Code;
            }
        }

        public string Name
        {
            get
            {
                return _Name;
            }
            set
            {
                _Name = value;
            }
        }

        public double Amount
        {
            set
            {
                _Amount = value;
            }
            get
            {
                return _Amount;
            }
        }
    }
}


View Code
----------

C#
<% foreach (var item in Model) { %>
    
        <tr>
            <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>
            <td>
                <%: item.Code %>
            </td>
            <td>
                <%: item.Name %>
            </td>
            <td>
                <%: String.Format("{0:F}", item.Amount) %>
            </td>
        </tr>
    
    <% } %>

    </table>
    
    <p>
        <%: Html.ActionLink("Create New", "Create") %>
    </p>
Posted

1 solution

Maybe:
C#
return View("DisplayCustomer", new Customer[] { cs });


Obviously your view is waiting for a collection, whereas you give it a single customer ; the piece of code I give will pass an array of Customer with one element, the one you created programmatically.
 
Share this answer
 
v2
Comments
Member 10370658 30-Oct-13 13:19pm    
Wow!!!!! Its works Now!!!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900