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
C# 查看用户ID,需要发布用户名_C#_Asp.net Mvc 3_View - Fatal编程技术网

C# 查看用户ID,需要发布用户名

C# 查看用户ID,需要发布用户名,c#,asp.net-mvc-3,view,C#,Asp.net Mvc 3,View,在我的Get/Index视图中,我将发布到holidayController中的Post/Index方法。 所以我在用户ID上发帖,我如何使用它来访问用户名 我试过了 public ViewResult Index(int userID, string userName) { Person person = new Person(); //this assigns person.Id to the user Id

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

我试过了

 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不能为null?

在函数定义中,可以在字符串userName之前使用ref关键字:
字符串用户名是否已填写?如果是这样,您可以使用person.Name=userName,如果它有一个公共setter。如果未填写字符串,如何存储这些对象?如果使用数据库,可以使用您提供的ID在数据库中查找Person对象。

您只需像这样初始化Person对象的名称

person.Name = userName
但是,我认为您的Name不能为null异常,因为您在Person类中的Name属性上有一个Require属性

可能传递给操作的用户名为NULL

再次阅读问题后,更新:

您可以简单地将用户名保存到TempData或会话,而不是在需要此数据的操作中进行访问

TempData["userName"] = person.Name;

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

ViewBag.UserName = person.Name


你为什么要这么做?我想你走错方向了……你是在使用默认内置的aspnetdb吗?@Pabloker,谢谢你的回答,你是对的,我走错方向了,最后,我在数据库中使用linq:Person=from p得到了它;用户名=person.Name@garfbradaz感谢你的回复,得到了排序请,再看一遍问题。他想给字符串用户名分配人名感谢回复最后使用LINQ对其进行排序请,再看一遍问题。他想给字符串用户名分配人名感谢回复,是不是走错了路,最后用LINQThanks来回复,但得到了排序
ViewBag.UserName = person.Name
ViewData["userName"] = person.Name