0
我正在用一捆SQL Server 2012和EF編寫一個asp.net mvc3項目。我爲此項目添加了授權。它創建用戶成功,但創建後,我希望它重定向到頁面顯示所有關於學生的信息,但我不明白我如何做到這一點。所有我發現它在我的視圖中使用的是這樣的:成功登錄後顯示用戶詳細信息
@User.Identity.Name
但這對我來說還不夠。我試圖發送身份證,但它返回給我一個錯誤。這裏是我的代碼:
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);
}
意見是開箱我的學生模型創建強類型查看/詳細信息,任何想法?
編輯
我添加到我的視圖代碼下一行,但我認爲這不是一個正確的解決方案
@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>
}
}