2

目前使用剃刀在mvc上工作,希望将我的会话值分配给模型,我可以知道如何完成吗?做了一些研究,但看到任何。

显示申请密码恢复的学生ID,并将学生ID保存在会话中,以便进一步处理并显示在密码恢复表中

            @Html.LabelFor(m => m.StudentId)
            @Html.DisplayFor(m=> m.StudentId)

当我希望将我的会话分配给 m.StudentId 时,有什么办法可以解决吗?

在控制器中

Session["StudentId"] = passwordrecovery.StudentId;
4

2 回答 2

1

您不需要将 id 存储在会话中。使用内置的会员提供程序功能来管理用户帐户。

除此之外,我建议将视图所需的任何内容/所有内容加载到视图包中,而不是依赖其他外部数据源。这可以使用 ActionFilters 从源(如 Sesssion)中提取值并将它们放入 ViewBag 或 View Model 中来完成。

于 2012-11-14T19:23:49.120 回答
1

If I understand you right, the best way would probably be to create a viewmodel, to which you assign the StudentId in your action before you return the view, rather than keeping the value in the session. You then pass the viewmodel to the view, and will have access to the StudentId in your view when creating your form.

// Create a view model that fit your needs
public class PasswordRecoveryViewModel {
    public int StudentId { get; set; }
    // Other properties as needed
}

// Do something like this in your action
public ActionResult YourAction () {
    var model = new PasswordRecoveryViewModel {
        StudentId = 1; // Assign the ID as needed
    }    
    return View(model);
}

Then at the top of your view you add this:

@Model YourNameSpace.PasswordRecoveryViewModel;

You will then have access to your student ID with @Html.LabelFor(m => m.StudentId)

于 2012-11-14T19:23:38.163 回答