如何在Mvc4中显示数据相关表 [英] How Do I Display Data Related Tables In Mvc4
问题描述
我有以下课程:
public partial class 类别
{
public 类别()
{
此 .CategoryUsers = new HashSet< CategoryUser>();
this .Entries = new HashSet< Entry>();
}
public int CategoryID {获得跨度>; set ; }
public string CatagoryName { get 跨度>; set ; }
public virtual ICollection< CategoryUser> CategoryUsers { get ; set ; }
public virtual ICollection< Entry>条目{获取; set ; }
}
public partial class 用户
{
public 用户()
{
此 .CategoryUsers = new HashSet< CategoryUser>();
this .Entries = new HashSet< Entry>();
}
public int UserID {获得跨度>; set ; }
[必需(ErrorMessage = 请提供全名,AllowEmptyStrings = false )]
public string FullName { get ; set ; }
[必需(ErrorMessage = 请提供用户名,AllowEmptyStrings = false )]
public string 用户名{获取; set ; }
[DataType(System.ComponentModel.DataAnnotations.DataType.Password)]
[StringLength( 50 ,MinimumLength = 6 ,ErrorMessage = 密码必须至少为6个字符。 )]
[必填]
public string 密码{< span class =code-keyword> get ; set ; }
[比较( 密码,ErrorMessage = 确认密码剂量不匹配。)]
[DataType(System.ComponentModel.DataAnnotations.DataType.Password) )]
[必填]
public string ConfirmPassword {获得跨度>; set ; }
public virtual ICollection< CategoryUser> CategoryUsers { get ; set ; }
public virtual ICollection< Entry>条目{获取; set ; }
}
public partial class CategoryUser
{
public int Category_User_ID {获取; set ; }
public int Fk_Category { get 跨度>; set ; }
public int Fk_User { get 跨度>; set ; }
public virtual 类别类别{获得跨度>; set ; }
public virtual 用户用户{ get < /跨度>; set ; }
}
public partial class 条目
{
public int EntryID {获取; set ; }
public string 标题{ get 跨度>; set ; }
public string 用户名{ get 跨度>; set ; }
public string 密码{ get 跨度>; set ; }
public string 网址{ get 跨度>; set ; }
public string 描述{ get 跨度>; set ; }
public int Fk_Category { get 跨度>; set ; }
public int Fk_User { get 跨度>; set ; }
public virtual 类别类别{获得跨度>; set ; }
public virtual 用户用户{ get < /跨度>; set ; }
}
用户控制器:
< pre lang =cs> public class UserController:Controller
{
[ HttpGet]
public ActionResult Index()
{
return 查看();
}
[HttpGet]
public ActionResult Register()
{
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult注册(用户用户)
{
if (ModelState.IsValid)
{
using (PasswordCloudEntities _db = new PasswordCloudEntities())
{
var count = _db.Users.Count(u = > u.Username == user.Username);
if (count == 0 )
{
_db.Users.Add(user);
_db.SaveChanges();
ModelState.Clear();
user = null ;
ViewBag.Message = 成功完成注册;
}
else
{
ViewBag.Message = 注册失败。用户名已存在。;
}
}
}
return PartialView(用户);
}
[HttpGet]
public ActionResult LogIn()
{
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult LogIn(用户用户)
{
if (IsValid(user.Username,user.Password))
{
FormsAuthentication.SetAuthCookie(user.Username) , false );
return RedirectToAction( Index, Home);
}
else
{
ModelState.AddModelError( , 登录详细信息错误。< /跨度>);
}
return PartialView(用户);
}
public ActionResult LogOut()
{
FormsAuthentication.SignOut();
return RedirectToAction( Index, Home);
}
private bool IsValid( string username, string password)
{
bool IsValid = false ;
使用( var db = new PasswordCloudEntities())
{
var user = db.Users.FirstOrDefault(u = > u.Username == username);
if (user!= null )
{
if (user.Password == password)
{
IsValid = true ;
}
}
}
return IsValid;
}
}
所以我是MVC的初学者,我已经让用户注册并登录,现在我需要根据记录的用户显示类别,然后从所选类别显示用户输入的条目。有人可以指导并帮助我完成后续步骤。提供要显示的屏幕的详细信息以及从中获取数据的表结构。
I have the following classes:
public partial class Category
{
public Category()
{
this.CategoryUsers = new HashSet<CategoryUser>();
this.Entries = new HashSet<Entry>();
}
public int CategoryID { get; set; }
public string CatagoryName { get; set; }
public virtual ICollection<CategoryUser> CategoryUsers { get; set; }
public virtual ICollection<Entry> Entries { get; set; }
}
public partial class User
{
public User()
{
this.CategoryUsers = new HashSet<CategoryUser>();
this.Entries = new HashSet<Entry>();
}
public int UserID { get; set; }
[Required(ErrorMessage = "Please provide full name", AllowEmptyStrings = false)]
public string FullName { get; set; }
[Required(ErrorMessage = "Please provide username", AllowEmptyStrings = false)]
public string Username { get; set; }
[DataType(System.ComponentModel.DataAnnotations.DataType.Password)]
[StringLength(50, MinimumLength = 6, ErrorMessage = "Password must be minimum 6 char long.")]
[Required]
public string Password { get; set; }
[Compare("Password", ErrorMessage = "Confirm password dose not match.")]
[DataType(System.ComponentModel.DataAnnotations.DataType.Password)]
[Required]
public string ConfirmPassword { get; set; }
public virtual ICollection<CategoryUser> CategoryUsers { get; set; }
public virtual ICollection<Entry> Entries { get; set; }
}
public partial class CategoryUser
{
public int Category_User_ID { get; set; }
public int Fk_Category { get; set; }
public int Fk_User { get; set; }
public virtual Category Category { get; set; }
public virtual User User { get; set; }
}
public partial class Entry
{
public int EntryID { get; set; }
public string Title { get; set; }
public string Username { get; set; }
public string Password { get; set; }
public string Url { get; set; }
public string Description { get; set; }
public int Fk_Category { get; set; }
public int Fk_User { get; set; }
public virtual Category Category { get; set; }
public virtual User User { get; set; }
}
The user controller:
public class UserController : Controller
{
[HttpGet]
public ActionResult Index()
{
return View();
}
[HttpGet]
public ActionResult Register()
{
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Register(User user)
{
if (ModelState.IsValid)
{
using (PasswordCloudEntities _db = new PasswordCloudEntities())
{
var count = _db.Users.Count(u => u.Username == user.Username);
if (count == 0)
{
_db.Users.Add(user);
_db.SaveChanges();
ModelState.Clear();
user = null;
ViewBag.Message = "Successfully Registration Done";
}
else
{
ViewBag.Message = "Registration failed. Username already exists.";
}
}
}
return PartialView(user);
}
[HttpGet]
public ActionResult LogIn()
{
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult LogIn(User user)
{
if (IsValid(user.Username, user.Password))
{
FormsAuthentication.SetAuthCookie(user.Username, false);
return RedirectToAction("Index", "Home");
}
else
{
ModelState.AddModelError("", "Login details are wrong.");
}
return PartialView(user);
}
public ActionResult LogOut()
{
FormsAuthentication.SignOut();
return RedirectToAction("Index", "Home");
}
private bool IsValid(string username, string password)
{
bool IsValid = false;
using (var db = new PasswordCloudEntities())
{
var user = db.Users.FirstOrDefault(u => u.Username == username);
if (user != null)
{
if (user.Password == password)
{
IsValid = true;
}
}
}
return IsValid;
}
}
So i am beginner in MVC, i've made user to register and log in, now i need depending of the logged user to display categories, and then from the selected category to display the entry that user entered. Can someone guide and help me with the next steps.
Give the details of the screen you want to display along with the table structure from where data is fetched.
这篇关于如何在Mvc4中显示数据相关表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!