我在MVC5中的客户模型的索引视图中创建了一个带有两个单选按钮的搜索栏。我想将搜索栏的内容,即已选择的单选按钮和已输入搜索栏的内容传递给我的CustomersController。然而,控制器不是"看到"视图中的名称。我做错了什么?
在索引视图中搜索条形码:
@using (Html.BeginForm("Index", "CustomersController", FormMethod.Get))
{
<b>Search by:</b>@Html.RadioButton("searchBy", "Company")<text> Company</text>
@Html.RadioButton("searchBy", "Last Name")<text>Last Name</text><br />
@Html.TextBox("Search");<input type="submit" value="Search" />
}
CustomersController中的Index ActionResult:
public ActionResult Index()
{
if (searchBy == "Company")
{
return View(db.Customers
.Where(x => x.Company.Contains == Search || Search == null).ToList());
}
else
{
return View(db.Customers.Where(x => x.LastName.StartsWith(Search)).ToList());
}
}
非常感谢任何帮助。感谢。
答案 0 :(得分:0)
如下所示更改剃刀视图,无需应用完整的控制器名称,只需传递“客户”
@using (Html.BeginForm("Index", "Customers", FormMethod.Get))
{
<b>Search by:</b>@Html.RadioButton("searchBy", "Company")<text> Company</text>
@Html.RadioButton("searchBy", "Last Name")<text>Last Name</text><br />
@Html.TextBox("Search");<input type="submit" value="Search" />
}
您的控制器方法如下所示通过两个参数,如下所示
public ActionResult Index(string searchby,string Search)
{
if (searchby == "Company")
{
return View(db.Customers.Where(x => x.Company.Contains == Search || Search == null).ToList());
}
else
{
return View(db.Customers.Where(x => x.LastName.StartsWith(Search)).ToList());
}
}
希望有所帮助......