简体   繁体   中英

Displaying a single record in the asp.net for the _layout.cshtml view

I'm out of ideas, but I guess it's because I'm a noob in asp.net. I have a MVC3 project and my idea was to have 2 sections:

在此处输入图片说明

Section Main is for displaying tables and news and all the staff that changes when entering different pages. Section Main_profile is for displaying profile info (name, email etc) for currently logged user. Here is the problem. I know I need to put the code for Main_profile displaying in the _Layout.cshtml, as I want that information to be shown all the time. I have tried this:

    <section id="main_profile">
        @Html.Action("Profile", "Person", new { name = User.Identity.Name })
    </section>

Profile is a method in my PersonController, which looks like this:

public ViewResult Profile(string name)
    {
        string person_n = name;
        return View(db.Persons.Where(s => s.Username.Equals(person_n)));
    }

At the end, there is a view for Profile:

@model IEnumerable<Bachelor.Models.Person>
<h2>Your profile</h2>
<table border = "1">
<tr>
    <th>
        Username
    </th>
    <th>
        Birthday
    </th>
    <th>
        Education
    </th>
    <th>
        Email
    </th>
</tr>
@foreach (var item in Model) {
<tr>
    <td>
        @Html.DisplayFor(modelItem => item.Username)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.Birthday)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.Education)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.Email)
    </td>
</tr>

But what I get is this "An unhandled exception of type 'System.StackOverflowException' occurred in Unknown Module."

Don't know if that may have something to do with it, but in the Main section, on some pages there is a table of records being displayed. What am I doing wrong here?

Set your Layout to null in your partial. What is doing is recursively calling itself:

 @{ Layout = null; }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM