I have a drop down list of employees for user selection. When a user selects an employee, I compare the value with what is in my database. If the selected value contains the name and surname of the employee, I must be able to display the corresponding employee ID.
My problem is that I use .Contains, and it only captures the names of employees, although they all use the same body, and there are no errors, such as extra spaces, because the list of employees filled with the same table, I compare her in order to find identifiers.
[HttpPost] public ActionResult Create(AppViewModel app, App appl, string selectedEmployee) { //Grabs the value of the selected employee. selectedEmployee = Request.Form["selectEmployee"].ToString(); try { //TODO: Add insert logic here var emp = Database.Session.Query<Employee>().AsEnumerable(); appl.AppOwnerID = (from e in emp where selectedEmployee.Contains(e.EmpFirstName) && selectedEmployee.Contains(e.EmpLastName) select e.EmplID).First(); using (ITransaction transaction = Database.Session.BeginTransaction()) { Database.Session.Save(appl); transaction.Commit(); } return RedirectToAction("Index"); } catch (Exception ex) { return View(); } }
For some reason, when I select certain employees, it can find its identifiers, and it works fine, but for other employees it returns "" according to its value, because the Contains method cannot find its first and last name.
UPDATE: Here my list is created and populated. Since you all mentioned the use of cropping, I added a tuning method when I add objects to my list, and also add it where I compare the selected value with the database values.
// GET: App/Create public ActionResult Create(string selectEmployee) { //Holds all employees var emp = Database.Session.Query<Employee>().AsEnumerable(); //Orders employees by last name. var empOrdered = emp.OrderBy(e => e.EmpLastName); //Formats Employees Names and creates array var empName = (from e in empOrdered select e.EmpFirstName + " " + e.EmpLastName).ToArray(); //List to hold employee names var empList = new List<string>(); //Loops through array to add names into list foreach (string empl in empName) { empList.Add(empl.Trim()); } ViewBag.selectEmployee = new SelectList(empList); var model = new AppViewModel(); return View(new AppViewModel()); }
Second update: Here is my opinion to go along with the controller.
@model App_Catalog.Models.AppViewModel @{ ViewBag.Title = "Create"; } <h2>Create</h2> @using (Html.BeginForm()) { @Html.AntiForgeryToken() <div class="form-horizontal"> <h4>AppViewModel</h4> <hr /> @Html.ValidationSummary(true, "", new { @class = "text-danger" }) <div class="form-group"> @Html.LabelFor(model => model.AppName, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.AppName, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.AppName, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @Html.LabelFor(m => m.Owner, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.DropDownList("selectEmployee", "Please Select an Employee") </div> </div>