Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 3 MVC控制器/视图删除模型属性_Asp.net Mvc 3 - Fatal编程技术网

Asp.net mvc 3 MVC控制器/视图删除模型属性

Asp.net mvc 3 MVC控制器/视图删除模型属性,asp.net-mvc-3,Asp.net Mvc 3,我正在创建一个小型MVC应用程序,并将用户对象从一个控制器传递到另一个控制器中的ActionResult方法。用户对象的属性之一是名为Properties的属性对象列表 难点在于:当用户对象最终传递到相关视图时,它的列表不包含任何属性 以下是设置: 用户类: public class User { public int Id {get;set;} public List<Property> Properties {get;set;} } HomeController p

我正在创建一个小型MVC应用程序,并将用户对象从一个控制器传递到另一个控制器中的ActionResult方法。用户对象的属性之一是名为Properties的属性对象列表

难点在于:当用户对象最终传递到相关视图时,它的列表不包含任何属性

以下是设置:

用户类:

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

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);
}
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 emAPI.model\u Objects.User
@{
ViewBag.Title=“UserHome”;
Layout=“~/Views/Shared/_Layout.cshtml”;
}
用户主页
@Model.Forename,以下是您的属性:
    @foreach(var属性在@Model.Properties中) {
  • 属性。名称
  • }
视图加载没有任何问题-
@Model.Forename
很好,但就
HomeController
而言
user.Properties
在收到它时是空的,尽管我知道它不是在
AccountController
发送它时


任何人提供的任何帮助或建议都将被感激地接受。

RedirectToAction
方法向浏览器返回
HTTP 302
响应,这将导致浏览器对指定操作发出
GET
请求。您不应该考虑将其中的复杂对象传递给下一个动作方法

在这种情况下,您可以将用户对象保留在
会话
变量中,并在其余位置访问它

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");
}

重定向时无法传递整个复杂对象。只有简单的标量参数

实现这一点的标准方法是通过发出表单身份验证cookie对用户进行身份验证,该cookie将允许您在所有后续操作中存储用户id。然后,如果在控制器操作中需要用户详细信息,如姓名或其他信息,则只需查询数据存储以使用id从存储位置检索用户。只需查看创建新ASP.NET MVC 3应用程序时帐户控制器的实现方式

因此:

在目标操作中,只需从
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属性的视图模型,然后将该视图模型传递给视图。视图不关心您的用户域模型,也不应该关心。此外,在viewmodel上,您不需要将集合设置为
列表。如果将其设置为数组
属性[]
,则可以获得更好的性能。模型绑定器不需要使用列表上的添加或删除函数,因此数组应该可以很好地工作。Darin,谢谢你,非常有用。很抱歉成为n00b!尝试改编一个MVC教程,这是我得到VeiwBag的地方。
[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); 
}