2014-12-03 6 views
0

다른 사용자의 사용자 이름에 액세스하는 방법을 내가 한 사용자 이름으로 이미 로그인했는지 묻고 싶습니다.다른 UserName을 가져 오는 방법은 무엇입니까?

사용자 테이블에 필드를 추가하는 컨트롤러가 있습니다. 내가 다른 사용자 프로필로 이동하려면 링크

<div> 
    <a href="Profile/Index/" style="color:black"><b>@item.Author</b></a> 
</div> 

를 클릭하면

public ActionResult Index() 
{ 
    var username = db.Users.Find(User.Identity.GetUserId()); 
    return View(username); 
} 

public ActionResult Edit() 
{ 

    ApplicationUser profile = db.Users.Find(User.Identity.GetUserId()); 
    if (profile == null) 
    { 
     return HttpNotFound(); 
    } 
    return View(profile); 
} 

[HttpPost] 
[ValidateAntiForgeryToken] 
public ActionResult Edit([Bind(Include = "Id, Nickname, FirstName, LastName, SecondName, City, Address, Description, TelNum")] ApplicationUser profile) 
{ 
    if (ModelState.IsValid) 
    { 
     var user = db.Users.Find(profile.Id); 
     if (user == null) 
     { 
      return HttpNotFound(); 
     } 
     user.UserName = User.Identity.GetUserName(); 
     user.FirstName = profile.FirstName; 
     user.SecondName = profile.SecondName; 
     user.LastName = profile.LastName; 
     user.Address = profile.Address; 
     user.City = profile.City; 
     user.TelNum = profile.TelNum; 
     user.Description = profile.Description; 
     db.Entry(user).State = EntityState.Modified; 
     db.SaveChanges(); 
     return Redirect("/Profile/Index/" + profile.Id); 
    } 
    return View(profile); 
} 

여기에 내가 원하는.

+0

'href = "Profile/Index /"'는'Index()'메소드로 리디렉션 할 것입니다. 예를 들어 'Details (int ID)'와 같이 표시하고자하는'User '의 ID 또는 다른 속성을 식별하는 매개 변수가있는 메소드가 필요하고'href = "Profile/Details/#"'로 호출하십시오. 사용자. –

답변

0

"사용자 이름에 액세스하지 마십시오." 이제 그것이 작동하는 방식입니다.

임의의 사용자를 기반으로 페이지를 렌더링 할 수있는 방법으로 응용 프로그램을 설계하고 표시되지 않는 중요한 정보가 있거나 사용자가 정보를 편집 할 수 없도록해야합니다 할 수있다.

이것은 사용자 이름이나 사용자 ID와 같은 다른 요소를 기반으로 사용자를 찾는 방법을 제공해야한다는 것을 의미합니다. 일반적으로 http://example.com/profiles/theuser

과 같이 URL에 전달됩니다. 그런 다음 profilescontroller.index 메소드에서 ID를 가져 와서 데이터베이스에서 사용자를 조회하고 해당 정보를 기반으로 페이지를 렌더링합니다. 그러나 이것은 여러분이 작성해야하는 모든 것입니다.

관련 문제