0

在我的 Get/Index 视图中,我将发布到 holidayController 中的 Post/Index 方法。所以我通过 UserID 发布,如何使用它来访问用户名?

我试过了

 public ViewResult Index(int userID, string userName)
        {
            Person person = new Person();
            //this assigns person.Id to the user Id
            person.Id = userID;
            //i want to assign name of person to the string userName
             userName = person.name;
}

这正在闪烁,person.name 不能为空?

4

3 回答 3

0

是否string userName填写?如果是这样,person.Name = userName如果它具有公共设置器,则可以使用。如果未填写字符串,您如何存储这些对象?如果您使用数据库,则可以使用您提供的 ID 在数据库中查找 Person 对象。

于 2012-12-19T11:45:51.987 回答
0

你只需要像这样初始化这个人的名字。

person.Name = userName

但是我认为您有 Name cannot be null 异常,因为您 的 Person 类的 Name 属性上有一个Require属性

并且可能传递给操作的用户名是 NULL


再次阅读问题后更新:

您可以简单地将用户名保存到 TempData 或 Session 中,然后在需要此数据的操作中访问。

TempData["userName"] = person.Name;

或者

Session["userName"] = person.Name;


或者,如果您的视图需要该数据,您可以将其保存到 ViewBag 或 ViewData。

ViewBag.UserName = person.Name

或者

ViewData["userName"] = person.Name
于 2012-12-19T11:49:22.627 回答
-1

您可以在函数定义中 使用refbefore 关键字:http: //msdn.microsoft.com/en-gb/library/14akc2c7 (v=vs.80).aspxstring userName

于 2012-12-19T11:44:35.060 回答