Rakstit Rakstit - 11 months ago 52
ASP.NET (C#) Question

Display user details after successfully login

I'm writing a asp.net mvc3 project with bundle of SQL Server 2012 and EF. I'm added the authorization to this project. It's create user successfully, but after creating I want that it's redirect to page where display all info about student, but I'm not understand how I make do this. All what I found it's use in my View is something like this:


But that's not enough for me. I was try to send the id, but it return me an error. Here is my code:

public ActionResult Register()
return View();

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;

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);

Views is out of the box Create/Detail with strongly typed View by my Student Model, Any Ideas?


I'm add to my View next lines of code, but I think it's not a correct solution

@model IEnumerable<Models.Entities.Student>

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


Hello there @name

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


Answer Source

Your ActionResult Index(int id) returns only one object or null (.FirstOrDefault(x => x.StudentId == id);) But you typed your view as a list of objects IEnumerable<Models.Entities.Student>.


1) Change top definition to @model Models.Entities.Student

2) Now you may access all properties you need using @Model statment: