MVC SelectList combining multiple columns in text field

前端 未结 5 1517
遇见更好的自我
遇见更好的自我 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:47

    I did this by modifying my View Model, here are my code:

    The View Model

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using MvcEsosNew.Models;
    using System.Web.Mvc;
    
    namespace MvcEsosNew.ViewModels
    {
        public class EntitlementViewModel
        {
            public int EntitlementCount { get; set; }
            public Entitlement Entitlement { get; set; }
            public SelectList Member { get; set; }
            public SelectList Job_Grade { get; set; }
            public SelectList Department { get; set; }
            public SelectList Esos_Batch { get; set; }
        }
    
        public class department_FullName
        {
            public int deptID { get; set; }
            public string deptCode { get; set; }
            public string deptName { get; set; }
            public string fullName { get { return deptCode + " - " + deptName; } }
        }
    }
    

    The Controller

    public void getAllDepartment(EntitlementViewModel entitlementVM)
            {
                var department = from Department in db.Departments.Where(D => D.Status == "ACTIVE").ToList()
                                 select new department_FullName
                                 {
                                     deptID   = Department.id,
                                     deptCode = Department.department_code,
                                     deptName = Department.department_name
                                 };
                entitlementVM.Department = new SelectList(department, "deptID", "fullName");
            }
    

    The View

         
    @Html.LabelFor(model => model.Entitlement.department_id)
    @Html.DropDownListFor(model => model.Entitlement.department_id, Model.Department, new { @class="form-control" }) @Html.ValidationMessageFor(model => model.Entitlement.department_id)

    The result:

提交回复
热议问题