嗨,大家好我是ASP.NET MVC的新手我收到错误请解决这个问题 [英] Hi guys I am new to ASP.NET MVC I am getting the error please solve this

查看:72
本文介绍了嗨,大家好我是ASP.NET MVC的新手我收到错误请解决这个问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的模特和视图模型









My models and view model




public class StudentViewModel
  {
      [Key]
      public int StudentID { get; set; }
      [StringLength(50)]
      public string FirstName { get; set; }
      [StringLength(50)]
      public string LastName { get; set; }
      [StringLength(50)]
      public string FatherName { get; set; }
      [StringLength(300)]
      public string Address { get; set; }

      public DateTime DOB { get; set; }
      [StringLength(1)]
      public char Gender { get; set; }
      [StringLength(1)]
      public char caste { get; set; }
      [StringLength(11)]
      public string MobileNo { get; set; }
      [StringLength(100)]
      public string CourseName { get; set; }
      [StringLength(50)]
      public string Duration { get; set; }
      [StringLength(50)]
      public string Timing { get; set; }
      public String Institute { get; set; }
      public int HighestQualification { get; set; }
      public string Standard { get; set; }
      [StringLength(100)]
      public string Subject { get; set; }
      [StringLength(100)]
      public string SchoolName { get; set; }
public virtual List<Student_Computer> ComputerStudents { get; set; }
      public virtual List<Student_tution > TutionStudents { get; set; }
  }







[Table("ComputerStudent")]
  public class Student_Computer
  {
      [Key]
      public int StudentID { get; set; }
      [StringLength (50)]
      public string  FirstName { get; set; }
      [StringLength(50)]
      public string LastName { get; set; }
      [StringLength(50)]
      public string FatherName { get; set; }
      [StringLength(300)]
      public string Address { get; set; }

      public DateTime  DOB { get; set; }
     [StringLength(1)]
      public char  Gender { get; set; }
      [StringLength(1)]
      public char caste { get; set; }
      [StringLength(11)]
      public string MobileNo { get; set; }
      [StringLength(100)]
      public string CourseName { get; set; }
      [StringLength(50)]
      public string Duration { get; set; }
      [StringLength(50)]
      public string Timing { get; set; }
      public String Institute { get; set; }
      public int HighestQualification { get; set; }


  }




[Table("TutionStudent")]
  public class Student_tution
  {
      [Key]
      public int StudentID { get; set; }
      [StringLength(50)]
      public string FirstName { get; set; }
      [StringLength(50)]
      public string LastName { get; set; }
      [StringLength(50)]
      public string FatherName { get; set; }
      [StringLength(300)]
      public string Address { get; set; }

      public DateTime DOB { get; set; }
      [StringLength(1)]
      public char Gender { get; set; }
      [StringLength(1)]
      public char caste { get; set; }
      [StringLength(11)]
      public string MobileNo { get; set; }
      [StringLength(10)]
      public string Standard { get; set; }
      [StringLength(100)]
      public string Subject { get; set; }
      [StringLength(100)]
      public string SchoolName { get; set; }
      [StringLength(50)]
      public string Timing { get; set; }



  }



我的DBCONTEXT


My DBCONTEXT

public class StudentDBContext :DbContext
    {
        public StudentDBContext()
            : base("name=MultisoftEntities")
        {
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            throw new UnintentionalCodeFirstException();
        }

        public virtual  DbSet<Student_Computer> Student_Computer { get; set; }
        public virtual DbSet<Student_tution> Student_tution { get; set; }

        public System.Data.Entity.DbSet<Real_MUlti.Models.StudentViewModel> StudentViewModels { get; set; }
    }



我的控制器


My controller

public class StudentViewModelsController : Controller
    {
        private StudentDBContext db = new StudentDBContext();

        // GET: StudentViewModels
        public ActionResult Index()
        {
            StudentViewModel student = new StudentViewModel();
          
            return View(db.StudentViewModels.ToList());
        }



我的观点


My view

@model IEnumerable<Real_MUlti.Models.StudentViewModel>

@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.FirstName)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.LastName)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.FatherName)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Address)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.DOB)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.MobileNo)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.CourseName)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Duration)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Timing)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Institute)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.HighestQualification)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Standard)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Subject)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.SchoolName)
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.FirstName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.LastName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.FatherName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Address)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.DOB)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.MobileNo)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.CourseName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Duration)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Timing)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Institute)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.HighestQualification)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Standard)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Subject)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.SchoolName)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.StudentID }) |
            @Html.ActionLink("Details", "Details", new { id=item.StudentID }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.StudentID })
        </td>
    </tr>
}

</table>





错误



我尝试过:



我收到错误EntityFramework.dll中出现'System.InvalidOperationException'类型的异常但未在用户代码中处理



附加信息:实体类型StudentViewModel不是当前上下文模型的一部分。



error

What I have tried:

I am getting error An exception of type 'System.InvalidOperationException' occurred in EntityFramework.dll but was not handled in user code

Additional information: The entity type StudentViewModel is not part of the model for the current context.

推荐答案

首先,当您发布问题时,您应该只提供相关内容码。要回答这个问题,我们每个表格实际上不需要多个字段。重现问题所需的代码越少,查找错误代码就越容易。



我认为主要问题是你没有导入适当的命名空间在您的视图中: asp.net - 如何导入Razor View Page中的命名空间? - 堆栈溢出 [ ^ ]。



此外,通过编辑项目文件,可以强制在构建时编译视图。主要的缺点是,如果你有很多视图,那么构建应用程序会慢得多。据我所知,DEBUG和RELEASE构建可能有不同的配置,在许多情况下这可能是最好的妥协。



Google:MVCBuildViews [ ^ ]
First of all, when you post a question, you should provide only pertinent code. To answer that question we really don't need more than a few fields per table. The less code required to reproduce a problem, the easier it would be to find faulty code.

I think that the main problem is that you don't import appropriate namespaces in your view: asp.net - How do I import a namespace in Razor View Page? - Stack Overflow[^].

Also, by editing project file, it is possible to force view to be compiled at build time. The main disadvantage is that if you have many views, it will be much slower to build the application. As far as I know, it is possible to have a different configuration for DEBUG and RELEASE builds which might be the best compromise in many cases.

Google: MVCBuildViews[^]


您应该学习尽快使用调试器。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。



调试器允许你跟踪执行逐行检查变量,你会看到它有一个停止做你期望的点。

调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - A初学者指南 [ ^ ]



调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。 />
当代码不做ex的时候您接近一个错误。



使用调试器查看哪些代码带来异常,问题肯定在参数中。
You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
When the code don't do what is expected, you are close to a bug.

Use the debugger to see which code bring the exception, problem certainly in parameters.


这篇关于嗨,大家好我是ASP.NET MVC的新手我收到错误请解决这个问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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