System.InvalidOperationException: the model element passed to the dictionary is of type

I am writing code that will create an object client and show it

Class1.cs is the model I am using:

using System;                       
using System.Collections.Generic;                       
using System.Linq;                      
using System.Web;                       

namespace WebApplication4.Models                        
{                       
    public class Class1                     
    {                       
        public class Customer                       
        {                       
            public int Id { set; get; }                     
            public string CustomerCode { set; get; }                        
            public double Amount { set; get; }                      


        }                       

    }                       
}                       

in the DefaultController section I have:

using System;           
using System.Collections.Generic;           
using System.Linq;          
using System.Web;           
using System.Web.Mvc;           


using WebApplication4.Models ;          


namespace WebApplication4.Models            
{           
    public class Default1Controller : Controller            
    {           
        //          
        // GET: /Default1/          
        public ActionResult Index()         

        {           
            Class1.Customer ob = new Class1.Customer();         

            ob.Id = 1001;           
            ob.CustomerCode = "C001";           
            ob.Amount = 900.23;         
            return View(ob);            
        }           
    }       
}       

I right-clicked ActionResult and added a view with the following code:

@model WebApplication4.Models.Class1

@{
    ViewBag.Title = "Index";
}

    <html>


    <head>
        <title>Title of the document</title>
    </head>

        <body>

            <div> 
                 The customer id is: <% = | Model.Id %>  < br/>
                 The customer Code  is: <% = | Model.CustomerCode  %>  < br/>
                 The customer Amount is: <% = | Model.Id %>  < br/>

            </div>
</body>

</html>

When I run the code, I get:

Exception Details: System.InvalidOperationException: The model element passed to the dictionary is of type WebApplication4.Models.Class1 + Customer, but this dictionary requires a model element of type WebApplication4.Models.Class1.


ok, I selected a class in class Class1.cs,

so now it only talks about the public class client.

and I also changed the namespace WebApplication4.Models

to the WebApplication4.Controller namespace in DefaultController1.cs,

Index.cshtml,

Class1 WebApplication4.Models

+3
1

Class1.Customer(), , Class1.

- , ?

, :

@model WebApplication4.Models.Class1.Customer

Edit
:

(/Models/Customer.cs)

namespace WebApplication4.Models
{
    public class Customer
    {
        public int Id { set; get; }
        public string CustomerCode { set; get; }
        public double Amount { set; get; }
    }
}

(/Controllers/Default1Controller.cs)

using System.Web.Mvc;
using WebApplication4.Models;

namespace WebApplication4.Controllers
{
    public class Default1Controller : Controller
    {
        public ActionResult Index()
        {
            var ob = new Customer
            {
                Id = 1001, 
                CustomerCode = "C001", 
                Amount = 900.23
            };

            return View(ob);
        }
    }
}

(/Views/Default1/Index.cshtml).
. @Model... <%= Model....%> -.

@model WebApplication4.Models.Customer

@{
    ViewBag.Title = "Index";
}

<html>
<head>
    <title>Title of the document</title>
</head>
<body>
    <div>
        The customer id is: @Model.Id  < br/>
        The customer Code  is: @Model.CustomerCode  < br/>
        The customer Amount is: @Model.Amount < br/>
    </div>
</body>
</html>

MyApp/Default1/Index

<html>
<head>
    <title>Title of the document</title>
</head>
<body>
    <div>
        The customer id is: 1001  < br/>
        The customer Code  is: C001  < br/>
        The customer Amount is: 900.23 < br/>
    </div>
</body>
</html>
+3

All Articles