0

I have a Ticket model with some basic ticket information.

What I want to do now is I want to be able to assign Agent to each Ticket.

For the Agent, I want to use the ASP.Net Identity Users which I already have.

I want to add property called AgentName and AgentID to this Ticket model.

In my Index View, I want to see a single Agent Name assigned to AgentName property and When I am editing a Ticket I want to have a drop down list of all available ASP.Net Identity Users (Agent Name) so that I can assign the ticket to.

I want to also have the ability to query all assigned Ticket to an specific Agent using AgentID so somehow I need to link each Ticket to an Agent using the AgentID.

How can I make this happen with code? Any help is appreciated.

So far I have this:

   Public class Ticket
{
    public int Id { get; set; }
    Display(Name = "Ticket comes from")]
    public string Source { get; set; }
}

In my ticket Edit(int id) HttpGet method I am fetching a Ticket information with the following code.

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

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

    return View(model);
}

What I have tried

I have tried changing my Ticket model to something like this

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 AgentName and AgentID
    //...something like this
    public string AgentName {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.
    public string AgentID {get; set;} 
}

To get all ASP.Net Identity User (Agents) I have something like this:

ViewBag.Agents = new SelectList(db.Users);

To display as drop down list in Edit mode I have something like this:

            <div class="form-group">
                @Html.LabelFor(model => model.AgentName, new { @class = "col-sm-4 control-label" })
                <div class="col-sm-8">
                    @Html.DropDownList("Agents", ViewBag.Agents as SelectList, String.Empty, new { @class = "form-control" })
                </div>
            </div>

But nothing is working. When I am editing a Ticket, the Agent drop down list display some weir character like shown in the picture here. I want this to be a list of User Name. enter image description here

DEBUG

@nurdyguy Put a debug stop on the line in the controller where you do ViewBag.Agents = new SelectList(db.Users); This is what I got:

enter image description here

CB4
  • 690
  • 3
  • 13
  • 25
  • Feels like an EntityFramework issue. Did you do the data migration? – nurdyguy Jul 05 '17 at 16:00
  • i did use migration to update database because i have changed my model – CB4 Jul 05 '17 at 16:01
  • I am not sure if my drop down list implementation is correct. Any thought? – CB4 Jul 05 '17 at 16:03
  • 1
    Put a debug stop on the line in the controller where you do `ViewBag.Agents = new SelectList(db.Users);` and see what you are passing in. EF identity generally doesn't want you to get all users because that could conceivably be a HUGE list. You may need to set up your own query to get all users. I also would recommend using a view model for that rather than lumping it into the viewbag but first things first. – nurdyguy Jul 05 '17 at 16:11
  • @nurdyguy Please see picture in DEBUG section of question. Put a debug stop on the line in the controller where you do `ViewBag.Agents = new SelectList(db.Users);` Note that I currently have two user created in my application. – CB4 Jul 05 '17 at 16:22
  • 1
    The text you are seeing is because the object has the default `.ToString()` method which is being invoked. Try just loading the list itself into the viewbag and then creating the dropdown in the view. Or, use one of the overloads for SelectList and load only the values you want, https://msdn.microsoft.com/en-us/library/system.web.mvc.selectlist(v=vs.118).aspx – nurdyguy Jul 05 '17 at 16:41
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/148435/discussion-between-cb4-and-nurdyguy). – CB4 Jul 05 '17 at 16:44

1 Answers1

1

This is not an Entity Framework issue, this is just the default behavior for DropDown when you give it a complex object. Try keeping your View code the same and try something like the following in your Controller:

// change Name to the display property you want to show up in the dropdown
//                               source,    value,  display
ViewBag.Agents = new SelectList( db.Users,  "Id",   "Name");

References:

DropDownList Property to display

ASP.NET MVC Html.DropDownList SelectedValue

RemedialBear
  • 644
  • 5
  • 15
  • Thanks. How do I save the selected user to my ticket when submitting? – CB4 Jul 05 '17 at 18:20
  • That depends on how you set up your Ticket model. You'll probably want to use a foreign key to the primary key of the Users table, and simply set the ```Ticket.Agent_Id``` field equal to the selected item from the dropdown. – RemedialBear Jul 05 '17 at 20:06