Reputation: 859
I am writing a code that will create a object customer and display 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; }
}
}
}
under Default1Controller 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 am getting :
Exception Details: System.InvalidOperationException: The model item passed into the dictionary is of type 'WebApplication4.Models.Class1+Customer', but this dictionary requires a model item of type 'WebApplication4.Models.Class1'.
ok, I took out the nestled class under Class1.cs,
so now it only says public class Customer.
and I also changed namespace WebApplication4.Models
to namespace WebApplication4.Controller under DefaultController1.cs,
but when I go under Index.cshtml, it says
The Type or namespace Class1 does not exist in the namespace WebApplication4.Models
Upvotes: 5
Views: 2947
Reputation: 107387
Your controller is instantiating an object of type Class1.Customer()
passed to the razor View, whereas your View has been told to expect a model of type Class1
.
You have a nested class - I'm not sure if this is intentional?
If it is, change the view to:
@model WebApplication4.Models.Class1.Customer
Edit
I've compiled and run this:
Model (/Models/Customer.cs)
namespace WebApplication4.Models
{
public class Customer
{
public int Id { set; get; }
public string CustomerCode { set; get; }
public double Amount { set; get; }
}
}
Controller (/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);
}
}
}
View (/Views/Default1/Index.cshtml
).
Note razor uses @Model...
not <%= Model....%>
like web forms.
@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>
Produces the following page at 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>
Upvotes: 4