0

我正在编写一个包含 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);
    }

视图是开箱即用的,我的学生模型使用强类型视图创建/详细信息,有什么想法吗?

编辑

我将下一行代码添加到我的视图中,但我认为这不是正确的解决方案

@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>

        }
    }
4

1 回答 1

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
....
于 2013-08-17T19:17:39.490 回答