Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Contains only working partially C#, MVC

I have a drop down list of employees for a user to select. When a user selects an employee, I compare the value to what's in my database. If the selected value contains the first and last name of an employee, then I'm supposed to be able to pull their respective employee ID.

My problem is that I'm using .Contains, and it's only catching some employee names, even though they are all using the same casing and there's no way that there could be errors such as extra spaces because the list of employees is populated by the same table I'm comparing it to in order to find the IDs.

 [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's able to find their IDs and it works perfectly, but for other employees it returns " " as it's value because the Contains method can't find their first and last name.

UPDATE: Here is where my list is created and populated. As all of you had mentioned using trim, I added the trim method for when I am adding objects in my list, along with adding it where I compare the selected value to 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's my view 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>
like image 377
bongo700 Avatar asked Oct 31 '22 20:10

bongo700


1 Answers

First - change your Controller signature to this:

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

When you do it MVC automaticaly bind selectEmployee variable and you don't need this string anymore:

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

About your query I belive that you're trying to do this:

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

As all already said here - could be that you have some spaces in your employe name and last name. Use Trim() function to make sure that it's not your problem.

appl.AppOwnerID = emp.FirstOrDefault(e => selectEmployee.Contains(e.EmpFirstName.Trim())
                  && selectEmployee.Contains(e.EmpLastName.Trim())).EmplID;
like image 75
teo van kot Avatar answered Nov 15 '22 06:11

teo van kot