Click here to Skip to main content
15,890,845 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I finally got dropdownlist info to save to database, the only problem is, it is the Id not the name that saves to database, any suggestions? Here is the code and hopefully copy of database info...

Controller Info:

public class TicketsController : Controller
   {

       // GET: Tickets

       Helpdesk2017Db _db = new Helpdesk2017Db();

       private void PopulateLists(Tickets model)
       {
           ViewData["Categories"] = new SelectList(_db.Categories, "Id", "CategoryName", model.Categories);


           ViewData["Priorities"] = new SelectList(_db.Priorities, "Id", "PriorityName", model.Priorities);


           ViewData["Site_Admins"] = new SelectList(_db.Site_Admins, "Id", "Name", model.Site_Admins);
       }

       // GET: Tickets
       public ActionResult Tickets()
       {
           var model = from a in _db.Tickets orderby a.Date ascending select a;

           return View(model);
       }

       // GET: Tickets/Details/5
       public ActionResult Details(int id)
       {
           var view = _db.Tickets.Single(v => v.Id == id);
           return View(view);
       }

       // GET: Tickets/Create
       public ActionResult Create()
       {

           var model = new Tickets();
           PopulateLists(model);


           return View("Create");
       }

       // POST: Tickets/Create
       [HttpPost]
       public ActionResult Create(Tickets model)
       {
           if (ModelState.IsValid)
           {
               PopulateLists(model);
               _db.Tickets.Add(model);
               _db.SaveChanges();
               // TODO: Add insert logic here
               return RedirectToAction("Tickets");
           }

           return View("Tickets");
       }


Model Info:

public class Tickets
   {
       public string Priorities { get; set; }
       public string Categories { get; set; }
       public string Site_Admins { get; set; }
       public int Id { get; set; }
       public DateTime Date { get; set; }
       public string Name { get; set; }
       public string Email { get; set; }
       public long Telephone { get; set; }
       public string Location { get; set; }
       public string Region { get; set; }
       public string ComputerName { get; set; }
       public long AssetTag { get; set; }
       public string Subject { get; set; }
       public string Body { get; set; }


Create View Info:

<div class="col-md-offset-2 col-md-10">
               @Html.DropDownListFor(m => m.Site_Admins, (SelectList)ViewData["Site_Admins"],"Assign To", new { @class = "form-control", })
               @Html.DropDownListFor(m => m.Priorities, (SelectList)ViewData["Priorities"], "Select Priority", new { @class = "form-control",  })
               @Html.DropDownListFor(m => m.Categories, (SelectList)ViewData["Categories"], "Select Category", new { @class = "form-control",  })
               <h2></h2>
               <hr />
           </div>
           <hr />
       </div>


List View info (pulled from database actual Table showing on list page)


Priorities 4

Categories 4

Site_Admins 1


other information omitted from List view due to sensitivity.

What I am wanting is not the Id numbers from dropdowns to show on table/list view, I want the actual selected value. Thanks for any advice.

What I have tried:

I tried switching the Id and the name values in the controller, however the drop downlist shows the Ids instead of the selected item names so I switched it back.
Posted
Updated 6-Apr-17 15:02pm
Comments
Timothy Heckler 6-Apr-17 20:47pm    
I am not a developer per title, just trying to learn this stuff, and this is the application I decided I would try to build while I learn.... Bit off more than I can chew for a newbie. I am doing my research and trying different things and "teaching myself" how to code.

1 solution

Well I guess I figured it out... In my controller View data code, I omitted the "Id" and just put in the "CategoryName", "PriorityName", and "Name" twice then the model.Categories etc etc... wow it was just that simple, call it once because that is what will save to database, and call it the second time to show in dropdown list... wow... nothing is ever that simple... is it?

private void PopulateLists(Tickets model)
       {
           ViewData["Categories"] = new SelectList(_db.Categories, "CategoryName", "CategoryName", model.Categories);


           ViewData["Priorities"] = new SelectList(_db.Priorities, "PriorityName", "PriorityName", model.Priorities);


           ViewData["Site_Admins"] = new SelectList(_db.Site_Admins, "Name", "Name", model.Site_Admins);
       }


notice the difference? It works... so I'll go with it.
 
Share this answer
 
v2

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