Click here to Skip to main content
15,881,803 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
First I want to know, do I use an id when design my viewmodel?I mean EF use its built in id or we must map our id that receive from user to that id??
second I want to pass user that in the first page enter his/her student Id , and the page will go to next page showing student with his grades in for example a grid view.

What I have tried:

public class Course
   {
       [Key]
       [DatabaseGenerated(DatabaseGeneratedOption.None)]
      public int CourseId { get; set; }
       public string Title { get; set; }
       public int Credits { get; set; }
       [Range(minimum:0,maximum:20)]
       public int Grade { get; set; }
       [Required]
       [Range(minimum: 0.5, maximum: 4)]
       public int Unit { get; set; }

       public virtual ICollection<Student> Students { get; set; }

   }

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace Models
{
    public class Student
    {
        public Student() : base()
        { }        
        [Key]
        [Required]
        public int stID { get; set; }
        [Required]
        [StringLength(maximumLength:50,MinimumLength =2)]
        public string FirstName { get; set; }
        [Required]
        [StringLength(maximumLength: 50, MinimumLength = 2)]
        public string LastName { get; set; }
        [Required]        
        public string Department { get; set; }
        [EmailAddress]
        [Required]
        public string EmailId { get; set; }
        public string Gender { get; set; }
        [Required]
        public DateTime? DOB { get; set; }
        public DateTime DateCreated
        {            set            {                value = DateTime.Now;            }
        }
        public string Address { get; set; }
        public string City { get; set; }
        public string State { get; set; }
        [Required]
        public string Username { get; set; }
        [Required]
        public string Password { get; set; }

        public virtual ICollection<Course> Courses { get; set; }
    }


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace ViewModel
{
    public class StudentGradesVM
    {
        [Required]
        [Key]
       public int stID { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Title { get; set; }
        [Key]
        public int CourseId { get; set; }
        public int Grade { get; set; }

    }
}

[HttpGet]
       public ActionResult RedirectStudent(int id)
       {
           if(ModelState.IsValid)
           {
               return RedirectToAction("Results","StudentResults",new { id = id });
           }


               return View(HttpNotFound(id.ToString()));


       }

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Models;
namespace SchoolApp.Controllers
{
    public class StudentResultsController : Controller
    {
        // GET: StudentResults
        public ActionResult Results(ViewModel.StudentGradesVM model)
        {
            if (ModelState.IsValid)
            {
                Student st = new Student
                {
                    FirstName = model.FirstName,
                    LastName = model.LastName,
                };
                Course crse = new Course
                {
                    CourseId = model.CourseId,
                    Grade = model.Grade,
                    Title = model.Title,
                };
                    
             }   

            }
            return View();
        }
    }
}
Posted
Updated 16-Jan-17 22:36pm
v2

1 solution

There is no way to use multiple model in the view. But definitely there is a way you can achieve the result using following methond:
1. create class which will return the combined data
C#
public class StudentCourses
    {
        public Student Student { get; set; }
        public Course Course { get; set; }
    }
    public class Student
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }
    public class Course
    {
        public int CourseId { get; set; }
        public string Grade { get; set; }
        public string Title { get; set; }
    }

2. Return combine data model to View
C#
 Student st = new Student
{
    FirstName = model.FirstName,
    LastName = model.LastName,
};
Course crse = new Course
{
    CourseId = model.CourseId,
    Grade = model.Grade,
    Title = model.Title,
};
var StudentCourses = new StudentCourses() { Student = st;  Course = crse};
return RedirectToAction("Results", "StudentResults", StudentCourses);

3. Use model in View
HTML
@using StudentCourses;

4. Usage to show data in HTML page
Student Information
HTML
@Model.Student.FirstName

Course Information
HTML
@Model.Course.Title

Regards,
Imdadhusen
 
Share this answer
 
Comments
maysam_p82 17-Jan-17 5:33am    
You mean I should create 3 viewmodels containing student,course,and studentcourses?
Sunasara Imdadhusen 17-Jan-17 6:18am    
Yes. this is only they way you can achieve!
maysam_p82 17-Jan-17 7:45am    
So I want to ask another question; is needed CourseId we declare in our viewmodel? So what is the built in id that is auto generated in sql tables?? That's some confused.
maysam_p82 20-Jan-17 5:56am    
What parameter I should pass to the controller from view?(Id or Model)

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