title: How to use MVC 5 SearchBox to get int ID while searching string CustomerName
body: <p><strong>My SQL Table:</strong></p>
  
  <pre><code>[PAYID] [int] IDENTITY(1,1) NOT NULL,
  [PaymentCustomer] [int] NOT NULL,
  [PaymentAmount] [int] NOT NULL,
  [PaymentLocation] [int] NOT NULL,
  [PaymentActive] [bit] NOT NULL
  </code></pre>
  
  <p><strong>My SQL Table Value:</strong></p>
  
  <pre><code>PAYID   PaymentCustomer PaymentAmount   PaymentLocation PaymentActive
  2   1   5   1   1
  3   2   5   2   1
  4   3   10  2   1
  </code></pre>
  
  <p><strong>In Payment/Index shows as:</strong></p>
  
  <pre><code>AAAA    5   Location 1  True
  BBBB    5   Location 2  True
  CCCC    0   Location 2  True
  </code></pre>
  
  <p>Now in Payment/Index I want to build SerchBox and search customer by name. I did similar searchbox in my Customer/Index and worked as shown below. But I cannot implement same method to my Payment/Index controller. How do I do that?</p>
  
  <p><strong>Customer/Index Controller:</strong></p>
  
  <pre><code>// GET: CUSTOMERS
  public ActionResult Index(string sortOrder, string currentFilter, string searchString, int? page)
  {
      //var cUSTOMERS = db.CUSTOMERS.Include(c =&gt; c.APPROVALAUTHORITY).Include(c =&gt; c.CRITERIA).Include(c =&gt; c.REQUESTFROM);
      //return View(cUSTOMERS.ToList());
  
  
      ViewBag.CurrentSort = sortOrder;
      ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "name_desc" : "";
      ViewBag.DateSortParm = sortOrder == "Date" ? "date_desc" : "Date";
  
      if (searchString != null)
      {
          page = 1;
      }
      else
      {
          searchString = currentFilter;
      }
  
      ViewBag.CurrentFilter = searchString;
  
      var customers = from s in db.CUSTOMERS
                     select s;
      if (!String.IsNullOrEmpty(searchString))
      {
          customers = customers.Where(s =&gt; s.CustomerName.Contains(searchString)
                                 || s.CustomerSurname.Contains(searchString));
      }
      switch (sortOrder)
      {
          case "name_desc":
              customers = customers.OrderByDescending(s =&gt; s.CustomerName);
              break;
          case "Date":
              customers = customers.OrderBy(s =&gt; s.CustomerRegistrationDate);
              break;
          case "date_desc":
              customers = customers.OrderByDescending(s =&gt; s.CustomerRegistrationDate);
              break;
          default:  // Name ascending 
              customers = customers.OrderBy(s =&gt; s.CustomerName);
              break;
      }
  
      int pageSize = 50;
      int pageNumber = (page ?? 1);
      return View(customers.ToPagedList(pageNumber, pageSize));
  }
  </code></pre>
  
  <p><strong>Cudtomer/Index.cshtml:</strong></p>
  
  <pre><code>&lt;p&gt;
      @using (Html.BeginForm("Index", "CUSTOMERS", FormMethod.Get))
      {
          &lt;div class="row"&gt;
              &lt;div class="col-xs-6"&gt;
                  &lt;div class="input-group"&gt;
                      &lt;span class="input-group-btn"&gt;
                          &lt;button class="btn btn-default" type="submit"&gt;Müşteri Ara:&lt;/button&gt;
                      &lt;/span&gt;
                      @Html.TextBox("SearchString", ViewBag.CurrentFilter as string, new { @class = "form-control" })
                  &lt;/div&gt;&lt;!-- /input-group --&gt;
              &lt;/div&gt;&lt;!-- /.col-lg-6 --&gt;
          &lt;/div&gt;&lt;!-- /.row --&gt;
  
      }
  &lt;/p&gt;
  </code></pre>
  
  <p><strong>Thank you. And Here is the PAYMENT/Index controller:</strong></p>
  
  <pre><code>// GET: PAYMENT
  public ActionResult Index(string sortOrder, string currentFilter, string searchString, int? page)
  {
      ViewBag.CurrentSort = sortOrder;
      ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "name_desc" : "";
      ViewBag.DateSortParm = sortOrder == "Date" ? "date_desc" : "Date";
  
      if (searchString != null)
      {
          page = 1;
      }
      else
      {
          searchString = currentFilter;
      }
  
      ViewBag.CurrentFilter = searchString;
  
      var customers = from s in db.CUSTOMERS
                      select s;
      if (!String.IsNullOrEmpty(searchString))
      {
          customers = customers.Where(s =&gt; s.CustomerName.Contains(searchString)
                                 || s.CustomerSurname.Contains(searchString));
  
          if (customers.GetEnumerator().MoveNext() == true)
          {
              int customeId = customers.SingleOrDefault(p =&gt; p.CustomerName.Contains(searchString)).CUSTID;
  
              var pAYMENT = db.PAYMENT.Include(p =&gt; p.CUSTOMERS).Include(p =&gt; p.LOCATION);
              pAYMENT = db.PAYMENT.Where(p =&gt; p.PaymentCustomer == customeId);
  
              int fLen = pAYMENT.Count();
              ViewBag.TextMessage = "Total " + fLen + " records were found.";
  
              switch (sortOrder)
              {
                  case "name_desc":
                      pAYMENT = pAYMENT.OrderByDescending(s =&gt; s.PaymentCustomer);
                      break;
                  default:  // Name ascending 
                      pAYMENT = pAYMENT.OrderBy(s =&gt; s.PaymentCustomer);
                      break;
              }
  
              int pageSize = 50;
              int pageNumber = (page ?? 1);
              return View(pAYMENT.ToPagedList(pageNumber, pageSize));
          }
          else
          {
              //var pAYMENT = db.PAYMENT.Include(p =&gt; p.CUSTOMERS).Include(p =&gt; p.LOCATION);
              var pAYMENT = db.PAYMENT.Include(p =&gt; p.CUSTOMERS).Include(p =&gt; p.LOCATION);
              pAYMENT = db.PAYMENT.Where(p =&gt; p.PaymentCustomer == 0);
  
              pAYMENT.DefaultIfEmpty();
              ViewBag.TextMessage = "No Data found...";
              return View(pAYMENT.ToList());
          }
      }
      else
      {
          var pAYMENT = db.PAYMENT.Include(p =&gt; p.CUSTOMERS).Include(p =&gt; p.LOCATION);
          int fLen = pAYMENT.Count();
           ViewBag.TextMessage = "Total " + fLen + " records were found.";
          return View(pAYMENT.ToList());
      }
  </code></pre>
  
  <p>}</p>