【问题标题】:Show personal profile in MVC在 MVC 中显示个人资料
【发布时间】:2020-05-02 15:46:54
【问题描述】:

这是表示用户配置文件属性的模型类。

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的初学者..请帮我解释一下细节..

【问题讨论】:

    标签: asp.net-mvc


    【解决方案1】:

    您必须制作一个 Profile Controller 并将 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;
        }
    }
    

    然后,您必须创建向用户显示所有元素的视图,因此在视图/配置文件中,创建一个名为 GetProfile?cshtml 的视图 (.cshtml),您只需使用在控制器中检索到的模型作为那:

    @model YourProjectNamespaceToTheProfileObject.Profile 
    
    @Html.Display(m => m.UserName)
    @Html.Display(m => m.Age)
    

    所以这是对您问题的回答,但我不得不说已经为 ASP.NET MVC 进行了身份验证(称为身份),我建议您存储出生日期而不是用户的年龄:-)

    【讨论】:

    • 感谢您的回答...但在这里,GetUserprofilMethodThatYouMade();我不明白..那是什么方法??
    • @ShibliSadik 这是从您的数据库中读取用户信息的方法......例如,您提供一个 id,然后您将获得包含所有数据的 Profile 对象。您必须在一个单独的类/层中进行此操作才能重用相同的方法,因为也许管理员可以选择用户并查看详细信息,因此对于代码重用,您需要这样做,不要放进入控制器的数据访问!
    • @ShibliSadik 你还好吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-18
    • 2021-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-27
    相关资源
    最近更新 更多