Click here to Skip to main content
15,040,743 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
}


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.
Posted
Updated 4-Jul-17 18:35pm

1 solution

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.
   
Comments
Jaxam 5-Jul-17 9:36am
   
why do we need a type int AssignedToUser? Is it because we can then assign the ticket to an user ID so that we can query it easier later to get all Ticket for an user?
Ramesh Kumar Barik 6-Jul-17 1:07am
   
A ticket can be assigned to one user and when you retrieve the ticket info then you can know to whom this ticket assigned.
Jaxam 6-Jul-17 12:59pm
   
how do i do this using the viewmodel approach instead of viewbag?
Ramesh Kumar Barik 6-Jul-17 13:04pm
   
Yes, ViewModel is the best approach.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900