如何在搜索字符串 CustomerName 时使用 MVC 5 SearchBox 获取 int ID

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

我的SQLTable:

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

我的SQLTable值:

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

在Payment/Index中显示为:

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

现在 Payment/Index 我想构建 SerchBox 并按名称搜索客户。我在我的 Customer/Index 中做了类似的搜索框,工作如下所示。但是我无法对我的 Payment/Index 控制器实施相同的方法。我该怎么做?

Customer/Index 控制器:

// 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>

谢谢。这是 PAYMENT/Index 控制器:

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

}

从搜索框中获取姓名

使用搜索框值从客户 table 中获取客户 ID。

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

使用paymentDetails列表显示。