0

小さなMVCアプリケーションを作成していて、コントローラーから別のコントローラーのActionResultメソッドにUserオブジェクトを渡しています。Userオブジェクトの属性の1つは、Propertiesと呼ばれるPropertyオブジェクトのリストです。

これが摩擦です:Userオブジェクトが最終的に関連するビューに渡されるとき、そのリストにはプロパティが含まれていません。

設定は次のとおりです。

ユーザークラス:

public class User
{
   public int Id {get;set;}
   public List<Property> Properties {get;set;}
}

AccountController

public ActionResult LogOn(int userId, string cryptedHash)
{
   //code to logOn (this works, promise)

   User user = dbContext.getUser(userId);
   //debugging shows the user contains the list of properties at this point

   return RedirectToAction("UserHome", "Home", user);
}

HomeController

public ActionResult UserHome(User user)
{
    ViewBag.Messaage = "Hello, " + user.Forename + "!";
    return View(user);  //debugging shows that user.Properties is now empty(!)
}

UserHome.cshtml表示

@model emAPI.Model_Objects.User

@{
    ViewBag.Title = "UserHome";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>UserHome</h2>
<div>
        @Model.Forename, these are your properties:
        <ul>
            @foreach (var property in @Model.Properties)
            {
                <li>property.Name</li>
            }
        </ul>
</div>

@Model.Forenameビューは問題なく読み込まれます-問題HomeControllerありませんが、送信されuser.Propertiesたときはそうではなかったとは思いますが、受信したときは空でしたAccountController

誰かが提供しなければならないどんな助けやアドバイスもありがたいことに受け取られるでしょう。

4

2 に答える 2

1

リダイレクト時に複合オブジェクト全体を渡すことはできません。単純なスカラー引数のみ。

これを実現する標準的な方法は、フォーム認証 Cookie を発行してユーザーを認証することです。これにより、後続のすべてのアクションでユーザー ID を保存できます。次に、コントローラーアクションで、名前などのユーザーの詳細が必要な場合は、データストアにクエリを実行して、id を使用して格納されている場所からユーザーを取得します。新しい ASP.NET MVC 3 アプリケーションを作成するときのアカウント コントローラーの実装方法を見てみましょう。

そう:

public ActionResult LogOn(int userId, string cryptedHash)
{
   //code to logOn (this works, promise)

   User user = dbContext.getUser(userId);
   //debugging shows the user contains the list of properties at this point

   // if you have verified the credentials simply emit the forms
   // authentication cookie and redirect:
   FormsAuthentication.SetAuthCookie(userId.ToString(), false);

   return RedirectToAction("UserHome", "Home");
}

ターゲット アクションでは、User.Identity.Nameプロパティからユーザー ID を取得するだけです。

[Authorize]
public ActionResult UserHome(User user)
{ 
    string userId = User.Identity.Name;

    User user = dbContext.getUser(int.Parse(userId));

    ViewBag.Messaage = "Hello, " + user.Forename + "!";
    return View(user); 
}

ああ、お願いします、ViewBag を使用しないでください。代わりにビュー モデルを使用してください。ビューがユーザーの名前を表示して歓迎することだけを考えている場合は、単純に forename プロパティを含むビュー モデルを作成し、このビュー モデルをビューに渡します。ビューは User ドメイン モデルを気にしませんし、そうすべきではありません。

于 2012-07-25T12:44:03.007 に答える
0

RedirectToActionメソッドはHTTP 302ブラウザに応答を返します。これにより、ブラウザGETは指定されたアクションを要求します。その中の複雑なオブジェクトを次のアクションメソッドに渡すことを考えるべきではありません。

この場合、ユーザーオブジェクトをSession変数に保持し、残りの場所でアクセスできる可能性があります。

public ActionResult LogOn(int userId, string cryptedHash)
{   
   User user = dbContext.getUser(userId);
   if(user!=null)
   {
       Session["LoggedInUser"]=user;
       return RedirectToAction("UserHome", "Home");
   }    
}

public ActionResult UserHome()
{
    var loggedInUser= Session["LoggedInUser"] as User;
    if(loggedInUser!=null)
    {
       ViewBag.Messaage = "Hello, " + user.Forename + "!";
       return View(user); 
    }
    return("NotLoggedIn");
}
于 2012-07-25T12:40:55.967 に答える