2017-12-12 5 views
1

mvcレイアウトビューを作成する必要があります。私は、ユーザーがjqueryコードを実行し、メソッドがjsonを返すメソッドにajaxポストを作成し、そのjsonをユーザー情報として使用する必要がある場合、ビューからチェックしています。 Index()メソッドが実行されているときに、現在のバッファが許可されたユーザーであるかどうかはわかりません。 Index()がロードされると、私は電子メールで自分のユーザー情報を送信するコントローラーに戻ることはできません。 問題を理解したらアドバイスをしてください。ユーザーが承認されている場合に表示するユーザーモデルデータを取得

私のコントローラの方法:怒鳴る

[Authorize] 
     [HttpPost] 
     public JsonResult GetUser(string email) 
     { 
      using (BlexzWebDbEntities db = new BlexzWebDbEntities()) 
      { 
       var data = db.Users.Where(x => x.Email == email).FirstOrDefault(); 
       return Json(data); 
      } 
     } 

コントローラコード:

public class DashboardController : Controller 
{ 
    // GET: Index of dashboard 
    [Authorize] 
    public ActionResult Index() 
    { 
     return View(); 
    } 
} 
怒鳴る

_Layout.cshtmlコード:

<!DOCTYPE html> 
<html> 
<head> 
    <meta charset="utf-8" /> 
    <meta name="viewport" content="width=device-width, initial-scale=1.0"> 
    <title>@ViewBag.Title</title> 
    @Scripts.Render("~/bundles/jquery") 
    @Scripts.Render("~/bundles/bootstrap") 


@{ 
    string Name = ""; 
    var Email = ""; 
    if (User.Identity.IsAuthenticated) 
    { 
     Email = System.Web.HttpContext.Current.User.Identity.Name; 

     <script> 
      $.post("/Dashboard/GetUser", { email: "@Email" }).done(function (data) { 
       console.log(data);//here i am receiving data which i dont want to 
      }); 
     </script> 

    } 
} 

</head> 
<body> 

</body> 
</html> 

さらにモデルサンプル怒鳴る:

public partial class User 
    { 
     [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")] 
     public User() 
     { 
      this.Transections = new HashSet<Transection>(); 
     } 

     public int UserId { get; set; } 
     public int RoleId { get; set; } 
     public string FirstName { get; set; } 
     public string LastName { get; set; } 
     public string Email { get; set; } 
     public string PasswordHash { get; set; } 
     public bool IsEmailVerified { get; set; } 
     public string EmailVerificationToken { get; set; } 
     public decimal Credit { get; set; } 
     public string AvatarName { get; set; } 

     [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")] 
     public virtual ICollection<Transection> Transections { get; set; } 
     public virtual Role Role { get; set; } 
    } 
+0

レイアウトで '@ Html.Action()'を使うと、ユーザが認証された場合に必要な詳細の部分的なビューを返す '[ChildActionOnly]'サーバメソッドを呼び出すことができます。そうではありません。 –

+0

例に答えることはできますか? @StephenMuecke –

+0

あなたはどんな情報を表示したいのですか( 'User'のどのプロパティ)? –

答えて

2

User詳細の一部のビューを返すサーバーメソッドを作成し、レイアウトに表示して@Html.Action()メソッドを使用してレンダリングします。例えば

[ChileActionOnly] 
public PartialViewResult UserDetails 
{ 
    if (User.Identity.IsAuthenticated) 
    { 
     User model = ... // your code to get the current user 
     return PartialView("_UserDetails", model); 
    } 
    else 
    { 
     return null; // assumes you do not want to display anything 
    } 
} 

とあなたの_UserDetails.cshtml部分

@model User 
<div>@Model.FirstName</div> 
.... other properties of User that you want to display 

とあなただけUserのいくつかのプロパティを表示する場合は、あなたではなくビューモデルを作成することを検討すべきであるレイアウト

@Html.Action("UserDetails", "yourControllerName"); 
// or @{ Html.Action("UserDetails", "yourControllerName"); } 

あなたのデータモデルをビューに戻すよりも。

関連する問題