如何在Asp.Net Mvc中解决此错误 [英] How Can I Solve This Error In Asp.Net Mvc

查看:56
本文介绍了如何在Asp.Net Mvc中解决此错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hi



这是我的代码:

hi

this is my code:

public ActionResult Index()
     {
       var model = (from r in _db.News.AsEnumerable()
                      select new
                      {
                          Body = r.Body,
                          Comments = r.Comments,
                          Education = r.Education,
                          Financial = r.Financial,
                          Id = r.Id,
                          ImageUrl = r.ImageUrl,
                          Notic = r.Notic,
                          Public = r.Public,
                          Refahi = r.Refahi,
                          Special = r.Special,
                          Summary = r.Summary,
                          Title = r.Title,
                          Visitors = r.Visitors,
                          CreateDate = (persian.GetYear(r.CreateDate) + "/" + persian.GetMonth(r.CreateDate) + "/" + persian.GetDayOfMonth(r.CreateDate)),
                          UpdateDate = (persian.GetYear(r.UpdateDate) + "/" + persian.GetMonth(r.UpdateDate) + "/" + persian.GetDayOfMonth(r.UpdateDate))
                      }).Take(6);

         return View(model);
     }





这个代码在视图中:



and this code is in view:

@model IEnumerable<News>
@foreach(var item in Model){
            <li class="clear">
             
              <div class="imgl"><img src="@item.ImageUrl" /></div>
              <div class="latestnews">
                <p><a href="#">@item.Title</a></p>
                <p>@item.Summary -- @item.UpdateDate</p>
              </div>
            </li>





给我这个错误:



give me this error:

 The model item passed into the dictionary is of type 'System.Linq.Enumerable+<TakeIterator>d__3a`1[<>f__AnonymousType4`15[System.String,System.Collections.Generic.ICollection`1[Nezam.Models.Comments],System.Boolean,System.Boolean,System.Int32,System.String,System.Boolean,System.Boolean,System.Boolean,System.Boolean,System.String,System.String,System.Collections.Generic.List`1[Nezam.Models.Visitors],System.String,System.String]]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[Nezam.Models.News]'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: The model item passed into the dictionary is of type 'System.Linq.Enumerable+<TakeIterator>d__3a`1[<>f__AnonymousType4`15[System.String,System.Collections.Generic.ICollection`1[Nezam.Models.Comments],System.Boolean,System.Boolean,System.Int32,System.String,System.Boolean,System.Boolean,System.Boolean,System.Boolean,System.String,System.String,System.Collections.Generic.List`1[Nezam.Models.Visitors],System.String,System.String]]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[Nezam.Models.News]'.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace: 

推荐答案

为您的新闻项创建一个viewmodel类。您的控制器代码是创建一个匿名对象(选择新... 的东西),这些对象无法传递给视图,因为您告诉视图它正在获取的IEnumerable<新闻> 。这不是您的控制器代码传递的内容。你传给它一个 IEnumberable< dynamic>



所以,为你的新闻对象创建一个视图模型类转而通过:

Create a viewmodel class for your News item. You're controller code is create an anonymous object (the select new... stuff) which cannot be passed to the view because you told the view it was getting an IEnumerable<news>. That's not what your controller code passed. You passed it an IEnumberable<dynamic>.

So, create a view model class for your News objects and pass that instead:
public class NewsViewModel
{
    public string Body { get; set; }
    public string Comments { get; set; }
    public string Education { get; set; }
    public string Financial { get; set; }
    public int Id { get; set; }
    public string ImageUrl { get; set; }
    public string Notice { get; set; }
    public string Public { get; set; }
    public string Refahi { get; set; }
    public string Special { get; set; }
    public string Summary { get; set; }
    public string Title { get; set; }
    public int Visitors { get; set; }
    public DateTime CreateDate  { get; set; }
    public DateTime UpdateDate { get; set; }
}





然后在你的控制器中,构建一个NewsViewModel对象的集合,就像你一样在构建匿名对象时执行:



Then in your controller, build a collection of the NewsViewModel objects, like you're doing when you built the anonymous object:

(from r in _db.News.AsEnumerable()
 select new NewsViewModel() {
 ...
 }).Take(6);



在您的视图中,更改您传入的模型类型:


And in your View, change the model type you're passing in:

@model IEnumerable<NewsViewModel>


错误在您使用的控制器中选择新的{...} 并且此代码生成匿名类型;所以你要传递给视图一个 IEnumberable< anonymous> ,你的视图期待一个类型 IEnumberable< news>



您应该在您的控制器和视图之间的通信中使用相同的模型类型,方法是创建(或使用)类似于解决方案中描述的类上面的1(在写答案时比我快)。
The error is in your controller where you are using select new {...} and this code generate an anonymous type; so you are passing to the view an IEnumberable<anonymous> and your view is expecting an model of type IEnumberable<news>.

You should use the same model type in communication between yours controller and view, by creating (or using) a class like is described in solution 1 above (was faster then me in writing the answer).


这篇关于如何在Asp.Net Mvc中解决此错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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