下面是表示用户配置文件属性的模型类。
public class Profile
{
public int ProfileId { get; set; }
public string UserName { get; set; }
public string Age { get; set; }
public string Gender { get; set; }
public string Password { get; set; }
public string Email { get; set; }
}当用户创建他们的配置文件时,他们的配置文件将保存在数据库中。当他们使用他们的电子邮件和密码登录时,我只想向他们显示他们的个人资料。我该怎么做呢?我是mvc..help me的初学者,请详细解释一下..
发布于 2015-07-13 18:40:04
您必须创建一个配置文件控制器,并将GetProfile作为方法。getProfile将加载所有用户数据并将其显示给客户。
Public Class Profile : Controller
{
pubmlic ActionResult GetProfile()
{
// get user id in session for instance, or somewhere else,
// here I put 5 for the example but it must be dynamic
int userId = 5;
// retrieve info and put it in model variable
Profile userprofil = GetUser(userId);
return View(model);
}
}其中GetUser(userId)是获取用户详细信息的入口点,例如
public static Class MyDataAccessLayerForUser
{
public static Profile GetUser(userId)
{
string SQL = "SELECT * from database WHERE user = @user";
// add @user parameter
// perform the query
...
Profile data = userReadOnDB;
return data;
}
}然后,您必须创建向user显示所有元素的视图,因此在Views/Profile中,创建一个名为GetProfile?cshtml的视图(.cshtml),您只需使用您在控制器中检索到的模型:
@model YourProjectNamespaceToTheProfileObject.Profile
@Html.Display(m => m.UserName)
@Html.Display(m => m.Age)这就是对您问题的回答,但我必须指出,已经为ASP.NET MVC进行了身份验证,我建议您存储出生日期而不是用户的年龄:-)
https://stackoverflow.com/questions/31381108
复制相似问题