MVC SelectList combining multiple columns in text field

前端 未结 5 1509
遇见更好的自我
遇见更好的自我 2020-12-04 19:12

How would I generate a select list, where the text field, is made up of two or more text columns, eg: Where I have a Description and Rate field in my database, I want to com

5条回答
  •  死守一世寂寞
    2020-12-04 19:40

    You can create a partial Model class

    public partial class Stand
    {
        public string DisplayName
        {
            get
            {
                return this.Description + "-- £" + this.Rate.ToString();
            }
        }
    
    }
    

    Then in your View

    var stands = db.Stands.Where(s => s.ExhibitorID == null).ToList();
    ViewBag.StandID = new SelectList(stands,"StandID", "DisplayName");
    

提交回复
热议问题