2

I'm trying to Select multiple values and assign them to a selectlist with specific option/value which is then passed via viewbag to my view, the problem I'm having is with the structure when its rendered and when in edit mode the value is not pre-populated.

I've done various searches to get me to this point but most use JQuery or other frameworks, Im looking to remain within C# / MVC it's just I'm now unsure of what phrase to search for in order to find an existing solution to this type of problem.

Can anyone give me a few pointers?

View:

<div class="form-group">
    @Html.LabelFor(model => model.employeeID, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.DropDownListFor(model => model.employeeID, ViewBag.employee as List<SelectListItem>, "Select", new { @class = "form-control" })
        @Html.ValidationMessageFor(model => model.employeeID, "", new { @class = "text-danger" })
    </div>
</div>

Controller :

 var employeeID = ((from x in dbdev.employeeDetails orderby x.employeeID select new { Name =  x.employeeNumber + x.knownas + x.surname, Value = x.employeeID } ).Distinct()).ToList();
 SelectList employeeIDList = new SelectList(employeeID);
 ViewBag.employeeID = employeeIDList;

Model:

[Required]
[Range(1, 2147483647)]
[Display(Name = "Employee ID")]
public int employeeID { get; set; }

Once my code is rendered the select has the following structure

<option>{ Name = 568883PhilShort, Value = 1 }</option>

but I'm actually trying to produce the following structure.

<option value="1">568883 Phil Short</option>

Below is after modification from comments

Model / HTML Unchanged

Updated controller based on comments:

 var employeesList = (from x in dbdev.employeeDetails.AsEnumerable()
                                     orderby x.employeeID ascending
                                     select new
                                     {
                                         Name = String.Format("{0} {1} {2}", x.employeeNumber, x.knownas, x.surname),
                                         Value = x.employeeID
                                     }).ToList();

                SelectList employeeIDList = new SelectList(employeesList, "Value", "Name", userAccess.employeeID);
                ViewBag.employeeID = employeeIDList;

Then to reduce the code I refactored to the following:

  ViewBag.employeeID = new SelectList((from x in dbdev.employeeDetails.AsEnumerable()
                                                            orderby x.employeeID ascending
                                                            select new
                                                            {
                                                                Name = String.Format("{0} {1} {2}", x.employeeNumber, x.knownas, x.surname),
                                                                Value = x.employeeID
                                                            }).ToList(), "Value", "Name", userAccess.employeeID);
tereško
  • 58,060
  • 25
  • 98
  • 150
Mr.B
  • 397
  • 5
  • 16
  • 2
    See https://stackoverflow.com/a/27437318/558486 – Rui Jarimba Oct 26 '18 at 08:36
  • problem is in this place select new { Name = x.employeeNumber + x.knownas + x.surname, Value = x.employeeID } – Eranga Gamagedara Oct 26 '18 at 08:38
  • 1
    You're not showing `ViewBag.applicationID` (and use whitespaces concatenation like `x.employeeNumber + " " + x.knownas + " " + x.surname`). Is that query is LINQ to Entities one? – Tetsuya Yamamoto Oct 26 '18 at 09:01
  • @Rui Jarimba Thanks for the link there is some useful information in there, going to take some time to absorb it fully. – Mr.B Oct 26 '18 at 09:36
  • @TetsuyaYamamoto Well spotted i'd copied the wrong html I've updated the question to reflect this. I avoided the white space concatenation though and went with string format (personal preference) – Mr.B Oct 26 '18 at 09:36

1 Answers1

2

You just need to correct a little so that the code will look like this:

var employeesList = dbdev.employeeDetails.OrderBy(x => x.employeeID)
                         .AsEnumerable()
                         .Select(x => new {
                              Name = String.Format("{0} {1} {2}", x.employeeNumber, x.knownas, x.surname), 
                              Value = x.employeeID
                         }).ToList();

Alternative by using query expression:

var employeesList = (from x in dbdev.employeeDetails.AsEnumerable()
                     orderby x.employeeID ascending
                     select new {
                         Name = String.Format("{0} {1} {2}", x.employeeNumber, x.knownas, x.surname), 
                         Value = x.employeeID
                     }).ToList();

Then create a SelectList using existing list:

SelectList employeeIDList = new SelectList(employeesList, "Value", "Name", userAccess.employeeID);

Or... just create a IEnumerable<SelectListItem> directly and pass it to DropDownListFor helper:

var employeesList = dbdev.employeeDetails.OrderBy(x => x.employeeID)
                         .AsEnumerable()
                         .Select(x => new SelectListItem {
                              Text = String.Format("{0} {1} {2}", x.employeeNumber, x.knownas, x.surname), 
                              Value = x.employeeID
                         }).ToList();
Tetsuya Yamamoto
  • 24,297
  • 8
  • 39
  • 61
  • Thankyou, that answer resolved both the value/select issue, I will update the above post with my end results and accept this answer. – Mr.B Oct 26 '18 at 10:50