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
{
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);
}
public ActionResult Tickets()
{
var model = from a in _db.Tickets orderby a.Date ascending select a;
return View(model);
}
public ActionResult Details(int id)
{
var view = _db.Tickets.Single(v => v.Id == id);
return View(view);
}
public ActionResult Create()
{
var model = new Tickets();
PopulateLists(model);
return View("Create");
}
[HttpPost]
public ActionResult Create(Tickets model)
{
if (ModelState.IsValid)
{
PopulateLists(model);
_db.Tickets.Add(model);
_db.SaveChanges();
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.