Click here to Skip to main content
15,909,742 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
My models and view model




C#
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; }
  }


C#
[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; }


  }

C#
[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; }



  }

My DBCONTEXT
C#
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
C#
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
HTML
@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>


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.
Posted
Updated 31-Aug-16 11:33am
Comments
[no name] 31-Aug-16 8:19am    
Learn how to use the debugger.

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[^]
 
Share this answer
 
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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900