我在我的MVC5客户模型的索引视图中创建了一个搜索栏,其中有两个单选按钮。我想将搜索栏的内容传递给我的CustomersController,即已经选择了哪个单选按钮以及在搜索栏中输入了哪些内容。然而,控制器并没有“看到”视图中的名称。我究竟做错了什么?在索引视图如何将视图中的搜索栏内容绑定到控制器MVC
搜索条码:
@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" />
}
指数的ActionResult在CustomersController:
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());
}
}
任何帮助将非常感激。谢谢。
你正在使用'GET'为什么没有PARAM参考'索引'方法?它应该是'Index(string searchBy)'。那么你从哪里获得'searchBy'? –