InvalidCast Exeption是由用户代码处理的? [英] InvalidCast Exeption was handled by the user code?

查看:54
本文介绍了InvalidCast Exeption是由用户代码处理的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是c#和asp.net的新手,它是一个单项目但是有点麻烦,我在名为FilmController.cs的控制器中使用以下代码,当我尝试编译它时给了我以下错误(InvalidCastExeption由用户代码处理)


此行上的
- 返回视图(filmRepository.Film.ToList());



代码如下



I am very new to c# and asp.net, it is a uni project but am having a little trouble, I am using thie following code in a controller named FilmController.cs, when I try to compile it is giving me the following error ( InvalidCastExeption was handled by the user code)

on this line - return View(filmRepository.Film.ToList ());

The code is as follows

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FilmMVC.Domain.Abstract;
using FilmMVC.Domain.Concrete;

namespace FilmMVC.WebUI.Controllers
{
    public class FilmController : Controller
    {
        private IFilmRepository filmRepository;
        public FilmController()
        {
            //temp hardcoded connection srting
            //until DI set up
            string connString = @"Data Source=172.30.10.170;
                                    Initial Catalog=30124003;
                                    User Id=30124003;
                                    Password=abc123#;
                                    MultipleActiveResultSets=True";
            filmRepository = new SqlFilmRepository(connString);
           
        }
        public ViewResult List()
        {
            return View(filmRepository.Film.ToList());
        }

    }
}





我的电影.c镜像我的数据库如下所示





My film.cs that mirrors my DB is as follows

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq.Mapping;


namespace FilmMVC.Domain.Entities
{   
    [Table(Name="film")]
    public class Film
    {
        [Column(IsPrimaryKey=true,
            IsDbGenerated=true,
            AutoSync=AutoSync.OnInsert)]
        public int id { get; set; }

        [Column] public string film_name { get; set; }
        [Column] public int film_releaseyear { get; set; }
        [Column] public string film_director { get; set; }
        [Column] public string film_certificate { get; set; }
        [Column] public string film_genre { get; set; }
        [Column] public string film_productionstudio { get; set; }
        [Column] public string film_imageurl { get; set; }
        [Column] public string film_synopsis { get; set; }
        [Column] public string film_info { get; set; }
        [Column] public string film_videourl { get; set; }
        
    }
}





我真的很感激帮助解决这个问题,因为它是非常令人沮丧。



非常感谢



I would really appreciate some help in solving this as it is very frustrating.

Thanks very much

推荐答案

没有相关的完整详情,也没有相关内容代码我只能引导你找到解决方案...

这段代码

Without the full details of the exception and without the relevant code I can only guide you towards the solution ...
This piece of code
public ViewResult List()

说''我有一个名为List的函数,它不带任何参数(参数)并且将返回ViewResult类型的值'。

我们遇到的第一个问题是你没有包含ViewResult的定义。



然而,继续......这一行代码说

is saying '"I have a function called "List" that doesn't take any arguments (parameters) and is going to return a value of the type "ViewResult"'.
First problem we have is you have not included the definition of ViewResult.

However, moving on... this line of code says

return View(filmRepository.Film.ToList());

'退出我的函数List返回运行函数的结果传递参数filmRepository.Film.ToList()时的查看''



我们遇到的第二个问题是你没有包含函数View的代码。它应该看起来像这样(对象可能是字符串)

'Out of my function "List" return the results of running function "View" when passed the argument "filmRepository.Film.ToList()"'

Second problem we have is you have not included the code for the function "View". It ought to look something like this (where object is probably string)

public ViewResult View(List<object> arg1)</object>



- 如果它没有返回一种ViewResult那么那就是你的问题。

- 如果它不期望List作为参数1那么那就是你的问题

- 如果它不是上述内容那么Film不支持ToList()(或者没有达到预期效果)


-- If it doesn't return a type of ViewResult then that is your problem.
-- If it is not expecting a List as argument 1 then that is your problem
-- If it is neither of the above then Film does not support ToList() (or doesn't do what you expect)


这篇关于InvalidCast Exeption是由用户代码处理的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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