如何将保存的下拉列表ID更改为所选项目名称? [英] How do I change saved dropdownlist id to selected item name?

查看:107
本文介绍了如何将保存的下拉列表ID更改为所选项目名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我终于得到dropdownlist信息保存到数据库,唯一的问题是,它是Id而不是保存到数据库的名称,有什么建议吗?这是代码,希望数据库信息的副本...



控制器信息:



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>





列表查看信息(从列表页面上显示的数据库实际表中提取)

< br $>


优先权4



分类4



Site_Admins 1





由于敏感度,列表视图中省略了其他信息。



我想要的不是从表格/列表视图中显示的下拉列表中的Id编号,我想要实际选择的值。感谢您的任何建议。



我的尝试:



我尝试在控制器中切换Id和名称值,但是下拉列表显示了ID而不是所选的项目名称,所以我将其切换回来。



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.

推荐答案

我好猜测我想通了......在我的控制器查看数据代码中,我省略了Id并只输入了CategoryName,PriorityName和Name两次,然后是model.Categories等等......哇它只是那么简单,称之为一次,因为这将保存到数据库,并在第二次调用它在下拉列表中显示...哇...没有什么是那么简单......是吗?



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.


这篇关于如何将保存的下拉列表ID更改为所选项目名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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