'/'应用程序中的服务器错误。传递到字典中的模型项是'MyFirstHelloWorld.Models.Customer'类型,但是这个字典需要一个类型为的模型项... [英] 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...

查看:163
本文介绍了'/'应用程序中的服务器错误。传递到字典中的模型项是'MyFirstHelloWorld.Models.Customer'类型,但是这个字典需要一个类型为的模型项...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好

我是asp.net mvc2的新手,但我有ASP.net c#的经验。现在使用Codeproject上发布的教程。

现在正在获取将值从控制器传递到视图时出错



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

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

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

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

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

推荐答案

可能:

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





显然你的观点正在等待收藏,而你给它一个顾客;我给出的代码片段将传递一个Customer数组,其中包含一个元素,即您以编程方式创建的元素。



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.


这篇关于'/'应用程序中的服务器错误。传递到字典中的模型项是'MyFirstHelloWorld.Models.Customer'类型,但是这个字典需要一个类型为的模型项...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆