添加用户到对象模型的列表(MVC5) [英] Add list of user to object model (MVC5)

查看:82
本文介绍了添加用户到对象模型的列表(MVC5)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的MVC5应用程序已经创建了一些已分配角色的用户。



我有一个带有一些基本票务信息的票证模型。



我现在要做的是想要为此Ticket模型添加一个名为 AssignedToUser 的额外属性。在我的索引视图中,我想看到分配给AssignedToUser属性的单个用户名,当我编辑故障单时,我希望有一个所有可用用户的下拉列表,以便我可以将故障单分配给。我怎样才能通过代码实现这一目标?感谢任何帮助。



My MVC5 application already has some users with assigned role created.

I have a Ticket model with some basic ticket information.

What I want to do now is I want to add an extra property called AssignedToUser to this Ticket model. In my Index View, I want to see a single User Name assigned to AssignedToUser property and When I am editing a Ticket I want to have a drop downlist of all available user so that I can assign the ticket to. How can I make this happen with code? Any help is appreciated.

Public class Ticket
{
	public int Id { get; set; }
    Display(Name = "Ticket comes from")]
	public string Source { get; set; }
	//....
	// Now I want to have a property called AssignedToUser
	//...something like this
	// Display(Name = "Ticket Assign To")]
	// public string AssignedToUser {get; set;} ***I want this to display the User Name of the User in Index View page. But, I also want a dropdown list of user when I am in Edit mode so that I can change the AssignedToUser to have different user.
}





我的Ticket Edit(int id)方法我目前使用Ticket ID查询Ticket并将其返回给查看。





On my Ticket Edit(int id) method I currently queried a Ticket using the Ticket ID and return it to View.

//HttpGet
     public ActionResult Edit(int id)
     {
         Models.Ticket model = db.Ticket
             .Include("Source")
             .Include("....")
             .SingleOrDefault(m => m.Id == id);

         if (model == null)
         {
             return new HttpNotFoundResult();
         }

         return View(model);
     }





我尝试过:



我试过谷歌搜索它但找不到直接给我的东西。



What I have tried:

I've tried googling it but unable to find something that's direct to me.

推荐答案

添加一个属性类型为AssignedToUser的属性和一个未映射的属性AssignedUserName(将保存该用户的名称)在故障单模型中的字符串类型。因此,一张票一次只能分配给一个用户。当您获取票证信息时,您可以设置AssignedToUser值。



对于用户下拉列表,您可以在操作方法中添加带有用户信息的SelectListItem并将其设置为一个VeiwBag将数据从控制器传递到视图。



Ex:for SelectListItem你可以参考这个链接。

c# - 如何让这个ASP.NET MVC SelectList工作? - 堆栈溢出 [ ^ ]



检查这可能对您有帮助。
Add one properties AssignedToUser of type int and one unmapped properties AssignedUserName (which will hold the name of that user) of string type in ticket model. So one ticket can assign to only one user at a time. When u fetch a ticket info that time you can set the AssignedToUser value.

For user dropdownlist you can add a SelectListItem in your action method with user information and set it in a VeiwBag to pass the data from controller to view.

Ex: for SelectListItem You can refer to this link.
c# - How can I get this ASP.NET MVC SelectList to work? - Stack Overflow[^]

Check this it may help you.


这篇关于添加用户到对象模型的列表(MVC5)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆