3

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());
    }

}

NTMS
  • 816
  • 7
  • 22

1 Answers1

1

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.

Vinoth
  • 851
  • 7
  • 23
  • Use the above code in controller same as like ur Customer/Index – Vinoth Nov 25 '15 at 03:35
  • `return View(paymentDetails.ToPagedList(pageNumber, pageSize));` – Vinoth Nov 25 '15 at 03:36
  • Here is what I did and get error that saying " Cannot implicitly convert type 'System.Linq.IQueryable' to int" var payments = from p in db.PAYMENT select p; int paymentcustomerId = customers.Where(p => p.CustomerName == searchString).Select(e => e.CUSTID); if (paymentcustomerId != null) { List paymentDetails = payments.Where(p => p.PaymentCustomer == paymentcustomerId).ToList(); } – NTMS Nov 25 '15 at 08:18
  • Wht is the data type of customerId? The problem is in int paymentcustomerId .change the paymentcustomerId type same as customerIds type – Vinoth Nov 25 '15 at 08:47
  • both is type int, I update my question (in last part PAYMENT/Index) – NTMS Nov 25 '15 at 09:19
  • k fine ...did u done debugging ..just sent some searchString value and find u are getting paymentcustomerId value or not – Vinoth Nov 25 '15 at 09:23
  • When I debug the customers has dana in it. But paymentcustomerId has no data only shows "Empty "Enumeration yielded no results" string" – NTMS Nov 25 '15 at 09:30
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/96123/discussion-between-vinoth-and-ntms). – Vinoth Nov 25 '15 at 09:32
  • Thank you Vinoth, I update my question and showd the correct search method. – NTMS Nov 26 '15 at 08:39