Contains only partially working C #, MVC

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> 
+5
source share
4 answers

First change your Controller signature to this:

 public ActionResult Create(AppViewModel app, App appl, string selectEmployee) 

When you do this, MVC automatically binds the selectEmployee variable, and you no longer need this line:

 //Grabs the value of the selected employee. selectedEmployee = Request.Form["selectEmployee"].ToString(); 

About your question I believe that you are trying to do this:

 appl.AppOwnerID = emp.FirstOrDefault(e => selectEmployee.Contains(e.EmpFirstName) && selectEmployee.Contains(e.EmpLastName)).EmplID; 

As already mentioned here, it may be that you have spaces in the name and surname of your employer. Use Trim() to make sure this is not your problem.

 appl.AppOwnerID = emp.FirstOrDefault(e => selectEmployee.Contains(e.EmpFirstName.Trim()) && selectEmployee.Contains(e.EmpLastName.Trim())).EmplID; 
+2
source

How about checking your Values โ€‹โ€‹Against Lower Case? For example (code can be optimized) as follows:

  where selectedEmployee.ToLowerCase().Contains(e.EmpFirstName.ToLowerCase()) && selectedEmployee.ToLowerCase().Contains(e.EmpLastName.ToLowerCase()) 
0
source

Perhaps draw the e.EmpFirstName and e.EmpLastName as they can be padded with spaces. I have seen this many times, and this can cause a lot of confusion.

0
source

Why don't you remove all the spaces and trim both sides on each line that you should check before checking the database.

 selectEmployee = selectEmployee.Replace(" ", "").Trim(); 

And do the same with the records you retrieve from the database before trying to match them using contains.

0
source

All Articles