gpt4 book ai didi

asp.net-mvc-5 - 如何在搜索字符串 CustomerName 时使用 MVC 5 SearchBox 获取 int ID

转载 作者:行者123 更新时间:2023-12-02 03:20:30 28 4
gpt4 key购买 nike

我的 SQL 表:

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

我的 SQL 表值:

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

在付款/索引中显示为:

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

现在在付款/索引中,我想构建 SerchBox 并按名称搜索客户。我在我的客户/索引中做了类似的搜索框,如下所示。但是我不能对我的支付/索引 Controller 实现相同的方法。我该怎么做?

客户/索引 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));
}

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

最佳答案

从搜索框获取名字

使用搜索框值从 Customer 表中获取 CustomerID。

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

使用paymentDetails列表显示。

关于asp.net-mvc-5 - 如何在搜索字符串 CustomerName 时使用 MVC 5 SearchBox 获取 int ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33891602/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com