我是MVC5的新手,并尝试整理一个小待办事项列表应用程序。我创建了一个包含用户名和密码的UserLogin表,以及一个包含用户所有其他信息的UserProfile表。这些表和相关功能我工作得很好。用户可以注册,登录,注销,更新适用的表格。
我还有一个do do list表,其中包含用户想要添加的项目。这是我遇到麻烦的地方。我无法弄清楚如何引用特定用户并显示特定于该用户的todolist表信息。
帐户管理员:
using MyToDoListApplication.Models.EntityManager;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using ToDoListApplicationNew.Models.DB;
using ToDoListApplicationNew.Models.ViewModel;
namespace ToDoListApplicationNew.Controllers
{
public class AccountController : Controller
{
// GET: Account
public ActionResult Index()
{
return View();
}
#region signup methods
// Get method for signup page
public ActionResult SignUpPage()
{
return View();
}
// Post method for signup page - post to db
[HttpPost]
// Pass in the UserSign up model object to be built
public ActionResult SignUpPage(UserSignUp USUV)
{
// Form is filled out and then method is entered
if (ModelState.IsValid)
{
// Form is filled out and database connection is established if form is valid
UserProfileManager UPM = new UserProfileManager();
if (!UPM.isLoginReal(USUV.Username))
{
// data access . adduseraccount from entity manager (where model objects are built)
UPM.AddUserAccount(USUV);
FormsAuthentication.SetAuthCookie(USUV.FirstName, false);
return RedirectToAction("Welcome", "Home");
}
else
{
}
}
return View();
}
#endregion
public ActionResult Login()
{
return View();
}
[HttpPost]
public ActionResult Login(UserLoginView ULV, string returnURL)
{
if(ModelState.IsValid)
{
UserProfileManager UPM = new UserProfileManager();
//retrive the password using the GetUserPassword() from UserProfileManager
// This will pass the user name to the gup method and if the username and password exist
// it will redirect the user to the welcome page.
string password = UPM.GetUserPassword(ULV.Username);
// If password is wrong or blank, catch with error message to user
if(string.IsNullOrEmpty(password))
{
ModelState.AddModelError("", "The user login or password provided is incorrect");
}
else
{
if(ULV.Password.Equals(password))
{
FormsAuthentication.SetAuthCookie(ULV.Username, false);
return RedirectToAction("_UserHome", "Account");
}
else
{
ModelState.AddModelError("", "The password provided is incorrect");
}
}
}
return View();
}
[Authorize]
public ActionResult SignOut()
{
FormsAuthentication.SignOut();
return RedirectToAction("Index", "Home");
}
#region UserHome
// After Login User is redirected to the _UserHome.cshtml view
// This is where listItems should be showing up
// MyList viewModel
public ActionResult UserHome()
{
if (Session["UserLoginID"] != null)
{
using (ToDoDBEntities db = new ToDoDBEntities())
{
return View(db.MyListItems.ToList());
}
}
else
{
return RedirectToAction("LogIn");
}
}
#endregion
}
}
这是我管理实体连接的课程:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ToDoListApplicationNew.Models.DB;
using ToDoListApplicationNew.Models.ViewModel;
namespace MyToDoListApplication.Models.EntityManager
{
public class UserProfileManager
{
public void AddUserAccount(UserSignUp newUser)
{
// create database connection
using (ToDoDBEntities db = new ToDoDBEntities())
{
// Collect viewmodel data
// Here building goes by object type and not foregin key relationship
UserLogin UL = new UserLogin();
UL.Username = newUser.Username;
UL.Password = newUser.Password;
// Add the UserLogin object I just built to the database
db.UserLogins.Add(UL);
db.SaveChanges();
UserProfile UP = new UserProfile();
// establish connection to UL by establishing foreign key relationship
UP.UserLoginID = UL.UserLoginID;
// Now add the new table properties to the new object
UP.FirstName = newUser.FirstName;
UP.LastName = newUser.LastName;
UP.CreationDate = newUser.CreationDate;
UP.Email = newUser.Email;
// Add UserProfile object to databse and save changes
db.UserProfiles.Add(UP);
db.SaveChanges();
}
}
//Check if user is real before login is allowed
public bool isLoginReal(string LoginName)
{
using (ToDoDBEntities DB = new ToDoDBEntities())
{
// Return the user from the DB whose login name matches the LoginName string passed in as perameter
return DB.UserLogins.Where(o => o.Username.Equals(LoginName)).Any();
}
}
// Check if password exists
public string GetUserPassword(string loginName)
{
using (ToDoDBEntities db = new ToDoDBEntities())
{
// retrieves user by comparing the username to the loginname passed in as a perameter
var user = db.UserLogins.Where(o => o.Username.ToLower().Equals(loginName));
if (user.Any())
return user.FirstOrDefault().Password;
else
return string.Empty;
}
}
}
}
这是我的UserHome视图。我想要做的是让用户在登录后重定向到这里并在此页面上显示他们的待办事项列表
@model IEnumerable<ToDoListApplicationNew.Models.DB.MyListItem>
@{
ViewBag.Title = "UserHome";
}
<h2>UserHome</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.ItemDate)
</th>
<th>
@Html.DisplayNameFor(model => model.Item)
</th>
<th>
@Html.DisplayNameFor(model => model.ItemImportance)
</th>
<th>
@Html.DisplayNameFor(model => model.UserProfile.FirstName)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.ItemDate)
</td>
<td>
@Html.DisplayFor(modelItem => item.Item)
</td>
<td>
@Html.DisplayFor(modelItem => item.ItemImportance)
</td>
<td>
@Html.DisplayFor(modelItem => item.UserProfile.FirstName)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.MyToDoListID }) |
@Html.ActionLink("Details", "Details", new { id=item.MyToDoListID }) |
@Html.ActionLink("Delete", "Delete", new { id=item.MyToDoListID })
</td>
</tr>
}
</table>
我还附上了数据库设计的图像。enter image description here
因此,我的登录和注册功能非常有用。我原来只是让他们重定向到了#34;欢迎&#34;本质上说用户的页面,欢迎。但就在我测试登录时。现在,我已经添加了第3个表来做列表项。我更新了edmx。我也有这个我目前没有使用的视图模型(虽然我知道如何做到这一点,但我已经构建了它:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
namespace ToDoListApplicationNew.Models.ViewModel
{
public class userListViewModel
{
[Key]
public int MyToDoListID { get; set; }
public int UserProfileID { get; set; }
[DataType(DataType.DateTime)]
[Display(Name =" Date of input")]
public DateTime ItemDate { get; set; }
[Required(ErrorMessage = "*")]
[Display(Name ="New Item")]
public string Item { get; set; }
[Required(ErrorMessage = "*")]
[Display(Name ="Importance level (1 - 10")]
public int ItemImportance { get; set; }
}
}
我真的很感激任何帮助。如果某些事情没有意义,请告诉我,我会尽力解释它。
干杯,
答案 0 :(得分:0)
您应该将此导航属性添加到userListViewModel类:
public UserProfile UserProfile{ get; set;}
在此之后,View应该可以工作(cshtml文件)。
但假设您使用EntityFramework,另一个问题可能是您的导航属性为空。 尝试更改UserHome操作,如下所示:
public ActionResult UserHome()
{
if (Session["UserLoginID"] != null)
{
using (ToDoDBEntities db = new ToDoDBEntities())
{
// Include fetches the data behind navigation properties
return View(db.MyListItems.Include(x => x.UserProfiles).ToList());
}
}
else
{
return RedirectToAction("LogIn");
}
}