成功登录后显示用户详细信息

时间:2013-08-17 08:12:59

标签: asp.net asp.net-mvc-3 razor authorization

我正在编写一个带有SQL Server 2012和EF捆绑的asp.net mvc3项目。我已将授权添加到此项目中。它是成功创建用户的,但在创建之后我希望它重定向到显示有关学生的所有信息的页面,但我不明白我是如何做到这一点的。我发现它在我的视图中使用的所有内容都是这样的:

@User.Identity.Name

但这对我来说还不够。我试着发送id,但它给我一个错误。这是我的代码:

public ActionResult Register()
    {
        return View();
    }

    [HttpPost]
    public ActionResult Register(Student student)
    {
        Register(student.Username, student.Password, student.Email, true, student.FirstName, student.LastName);
        return RedirectToAction("Index", "Membership", new {id == student.StudentId});
    }

    public static MembershipCreateStatus Register(string Username, string Password, string Email, bool IsApproved, string FirstName, string LastName) {
        MembershipCreateStatus CreateStatus;
        System.Web.Security.Membership.CreateUser(Username, Password, Email, null, null, IsApproved, null, out CreateStatus);

        if (CreateStatus == MembershipCreateStatus.Success) {
            using (UniversityContext Context = new UniversityContext(ConfigurationManager.ConnectionStrings[0].ConnectionString)) {
                Student User = Context.Students.FirstOrDefault(Usr => Usr.Username == Username);
                User.FirstName = FirstName;
                User.LastName = LastName;
                Context.SaveChanges();
            }

            if (IsApproved) {
                FormsAuthentication.SetAuthCookie(Username, false);
            }
        }

        return CreateStatus;
    }

public ActionResult Index(int id)
    {
        var model = context.Students
            .Include(x => x.Universities)
            .Include(x => x.FieldStudies)
            .Include(x => x.StudyModes)
            .Include(x => x.StudentDegrees)
            .Include(x => x.EntryMonths)
            .Include(x => x.Genders)
            .Include(x => x.EnglishLanguages)
            .Include(x => x.RussianLanguages)
            .Include(x => x.LatvianLanguages)
            .Include(x => x.OtherLans)
            .Include(x => x.OtherLanNexts)
            .FirstOrDefault(x => x.StudentId == id);

        return View(model);
    }

视图是开箱即用的创建/详细信息,我的学生模型,任何想法的强类型视图?

修改

我添加到我的View下一行代码,但我认为这不是一个正确的解决方案

@model IEnumerable<Models.Entities.Student>

@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_SecondLayout.cshtml";
    var name = @User.Identity.Name;
}

<h2>Index</h2>


Hello there @name

<fieldset>
    <legend>Student</legend>
    @foreach(var item in Model)
    {
        if (item.Username == name)
        {
            <div class="display-label">
                 @Html.DisplayNameFor(model => model.Username)
            </div>
            <div class="display-field">
                @Html.DisplayFor(model => item.Username)
            </div>

        }
    }

1 个答案:

答案 0 :(得分:0)

您的ActionResult Index(int id)只返回一个对象或null(.FirstOrDefault(x => x.StudentId == id);)但是您将视图键入为对象列表IEnumerable<Models.Entities.Student>.

所以,

1)将顶部定义更改为@model Models.Entities.Student

2)现在,您可以使用@Model声明:

访问所需的所有媒体资源
@Model.username
@Model.email
@Model.FirstName
@Model.SeconName
....
相关问题