Search code examples
asp.net-mvc-5search-box

How to use MVC 5 SearchBox to get int ID while searching string CustomerName


My SQL Table:

[PAYID] [int] IDENTITY(1,1) NOT NULL,
[PaymentCustomer] [int] NOT NULL,
[PaymentAmount] [int] NOT NULL,
[PaymentLocation] [int] NOT NULL,
[PaymentActive] [bit] NOT NULL

My SQL Table Value:

PAYID   PaymentCustomer PaymentAmount   PaymentLocation PaymentActive
2   1   5   1   1
3   2   5   2   1
4   3   10  2   1

In Payment/Index shows as:

AAAA    5   Location 1  True
BBBB    5   Location 2  True
CCCC    0   Location 2  True

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?

Customer/Index Controller:

// GET: CUSTOMERS
public ActionResult Index(string sortOrder, string currentFilter, string searchString, int? page)
{
    //var cUSTOMERS = db.CUSTOMERS.Include(c => c.APPROVALAUTHORITY).Include(c => c.CRITERIA).Include(c => 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 => s.CustomerName.Contains(searchString)
                               || s.CustomerSurname.Contains(searchString));
    }
    switch (sortOrder)
    {
        case "name_desc":
            customers = customers.OrderByDescending(s => s.CustomerName);
            break;
        case "Date":
            customers = customers.OrderBy(s => s.CustomerRegistrationDate);
            break;
        case "date_desc":
            customers = customers.OrderByDescending(s => s.CustomerRegistrationDate);
            break;
        default:  // Name ascending 
            customers = customers.OrderBy(s => s.CustomerName);
            break;
    }

    int pageSize = 50;
    int pageNumber = (page ?? 1);
    return View(customers.ToPagedList(pageNumber, pageSize));
}

Cudtomer/Index.cshtml:

<p>
    @using (Html.BeginForm("Index", "CUSTOMERS", FormMethod.Get))
    {
        <div class="row">
            <div class="col-xs-6">
                <div class="input-group">
                    <span class="input-group-btn">
                        <button class="btn btn-default" type="submit">Müşteri Ara:</button>
                    </span>
                    @Html.TextBox("SearchString", ViewBag.CurrentFilter as string, new { @class = "form-control" })
                </div><!-- /input-group -->
            </div><!-- /.col-lg-6 -->
        </div><!-- /.row -->

    }
</p>

Thank you. And Here is the PAYMENT/Index controller:

// 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 => s.CustomerName.Contains(searchString)
                               || s.CustomerSurname.Contains(searchString));

        if (customers.GetEnumerator().MoveNext() == true)
        {
            int customeId = customers.SingleOrDefault(p => p.CustomerName.Contains(searchString)).CUSTID;

            var pAYMENT = db.PAYMENT.Include(p => p.CUSTOMERS).Include(p => p.LOCATION);
            pAYMENT = db.PAYMENT.Where(p => p.PaymentCustomer == customeId);

            int fLen = pAYMENT.Count();
            ViewBag.TextMessage = "Total " + fLen + " records were found.";

            switch (sortOrder)
            {
                case "name_desc":
                    pAYMENT = pAYMENT.OrderByDescending(s => s.PaymentCustomer);
                    break;
                default:  // Name ascending 
                    pAYMENT = pAYMENT.OrderBy(s => s.PaymentCustomer);
                    break;
            }

            int pageSize = 50;
            int pageNumber = (page ?? 1);
            return View(pAYMENT.ToPagedList(pageNumber, pageSize));
        }
        else
        {
            //var pAYMENT = db.PAYMENT.Include(p => p.CUSTOMERS).Include(p => p.LOCATION);
            var pAYMENT = db.PAYMENT.Include(p => p.CUSTOMERS).Include(p => p.LOCATION);
            pAYMENT = db.PAYMENT.Where(p => p.PaymentCustomer == 0);

            pAYMENT.DefaultIfEmpty();
            ViewBag.TextMessage = "No Data found...";
            return View(pAYMENT.ToList());
        }
    }
    else
    {
        var pAYMENT = db.PAYMENT.Include(p => p.CUSTOMERS).Include(p => p.LOCATION);
        int fLen = pAYMENT.Count();
         ViewBag.TextMessage = "Total " + fLen + " records were found.";
        return View(pAYMENT.ToList());
    }

}


Solution

  • Get the Name from searchbox

    Take the CustomerID from the Customer table using searchbox value.

    int customeId = customers.Single(p => p.CustomerName.Contains(searchString)).CUSTID;
    if(paymentcustomerId!= null)
    {
     List<Payment> paymentDetails=payment.where(p=>p.PaymentCustomer ==paymentcustomerId).ToList();    
    }
    

    Use the paymentDetails list to display.