如何将两个表中的viewmodel详细信息传递到mvc5中的视图? [英] How can I pass viewmodel details from two tables into a view in mvc5?
问题描述
首先我想知道,在设计我的viewmodel时是否使用id?我的意思是EF使用其内置ID或者我们必须将从用户接收的id映射到该ID?
第二,我想通过用户在第一页输入他/她的学生ID,页面将转到下一页,显示学生的成绩,例如网格视图。
我尝试过:
公共课课程
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int CourseId {get;组; }
public string Title {get;组; }
public int Credits {get;组; }
[范围(最小值:0,最大值:20)]
public int Grade {get;组; }
[必需]
[范围(最小值:0.5,最大值:4)]
public int Unit {get;组; }
public virtual ICollection< Student>学生{得到;组; }
}
使用System;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Data.Entity;
使用System.ComponentModel.DataAnnotations;
使用System.ComponentModel.DataAnnotations.Schema;
命名空间模型
{
公共类学生
{
公共学生():base()
{}
[Key]
[必需]
public int stID {get;组; }
[必需]
[StringLength(maximumLength:50,MinimumLength = 2)]
public string FirstName {get;组; }
[必需]
[StringLength(maximumLength:50,MinimumLength = 2)]
public string LastName {get;组; }
[必需]
公共字符串部门{get;组; }
[EmailAddress]
[必填]
公共字符串EmailId {get;组; }
public string Gender {get;组; }
[必填]
公共日期时间? DOB {get;组; }
public DateTime DateCreated
{set {value = DateTime.Now; }
}
公共字符串地址{get;组; }
public string City {get;组; }
public string State {get;组; }
[必需]
公共字符串用户名{get;组; }
[必填]
公共字符串密码{get;组; }
公共虚拟ICollection<课程>课程{get;组; }
}
使用System;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.ComponentModel.DataAnnotations;
使用System.ComponentModel.DataAnnotations.Schema;
namespace ViewModel
{
public class StudentGradesVM
{
[必需]
[Key]
public int stID {get;组; }
公共字符串FirstName {get;组; }
公共字符串LastName {get;组; }
public string Title {get;组; }
[Key]
public int CourseId {get;组; }
public int Grade {get;组; }
}
}
[HttpGet]
public ActionResult RedirectStudent(int id)
{
if(ModelState.IsValid)
{
return RedirectToAction(Results,StudentResults,new {id = id});
}
返回视图(HttpNotFound(id.ToString()));
}
使用System;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.Mvc;
使用模型;
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,
};
课程crse =新课程
{
CourseId = model.CourseId,
Grade = model.Grade,
Title = model.Title,
};
}
}
return View();
}
}
}
在视图中无法使用多个模型。但是肯定有一种方法可以使用以下methond来实现结果:
1.创建将返回组合数据的类
< span class =code-keyword> public class StudentCourses
{
public 学生{获取; set ; }
public 课程课程{ get ; set ; }
}
public class 学生
{
public string FirstName { get ; set ; }
public string LastName { get 跨度>; set ; }
}
public class 课程
{
public int CourseId { get ; set ; }
public string 成绩{获取跨度>; set ; }
public string 标题{ get 跨度>; set ; }
}
2.将组合数据模型返回到View
学生st = new 学生
{
FirstName = model.FirstName,
LastName = model.LastName,
};
课程crse = new 课程
{
CourseId = model.CourseId,
Grade = model.Grade,
Title = model.Title,
};
var StudentCourses = new StudentCourses(){Student = st; Course = crse};
return RedirectToAction( Results, StudentResults,StudentCourses);
3.在视图中使用模型
@使用StudentCourses;
4.用于显示数据在HTML页面
学生信息
@ Model.Student.FirstName
课程信息
@ Model.Course.Title
问候,
Imdadhusen
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(); } } }
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
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
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
@using StudentCourses;
4. Usage to show data in HTML page
Student Information
@Model.Student.FirstName
Course Information
@Model.Course.Title
Regards,
Imdadhusen
这篇关于如何将两个表中的viewmodel详细信息传递到mvc5中的视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!