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);
5
  • 2
    See stackoverflow.com/a/27437318/558486 Commented Oct 26, 2018 at 8:36
  • problem is in this place select new { Name = x.employeeNumber + x.knownas + x.surname, Value = x.employeeID } Commented Oct 26, 2018 at 8: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? Commented Oct 26, 2018 at 9:01
  • @Rui Jarimba Thanks for the link there is some useful information in there, going to take some time to absorb it fully. Commented Oct 26, 2018 at 9: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) Commented Oct 26, 2018 at 9:36

1 Answer 1

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();
Sign up to request clarification or add additional context in comments.

1 Comment

Thankyou, that answer resolved both the value/select issue, I will update the above post with my end results and accept this answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.