几个小时以来我一直陷入这个问题。当我单击“提交”按钮时,它没有响应。只是刷新页面。
索引控制器
public ActionResult Index()
{
return View(db.Students.ToList());
}
[HttpGet,ActionName("Index")]
public ActionResult SearchIndex(string option, string search)
{
if (option == "Name")
{
var a = db.Students.Where(x => x.StudentName == search || search == null);
return View(a.ToList());
}
else if (option == "Gender")
{
return View(db.Students.Where(x => x.Gender == search).ToList());
}
else
{
return View(db.Students.Where(x => x.RegNo == search || search == null).ToList())
}
}
索引视图
@using(Html.BeginForm("Index","Student",FormMethod.Get)){
<div id="search">
<b>Search By:</b>@Html.RadioButton("option","Name")<b>Name</b>
@Html.RadioButton("option","Gender")<b>Gender</b>
@Html.RadioButton("option","Dept")<b>Dept</b>
@Html.RadioButton("option","RegNo")<b>RegNo</b>
<input type="text" name="text" />
<input type="submit" name="submit" value="Search" class="btn btn-default"/>
</div>
}
如何解决该问题?
答案 0 :(得分:0)
我认为这段代码可以提供更多结果
public ActionResult SearchIndex(string search)
{
var students = from s in db.Students
select s;
if (!String.IsNullOrEmpty(search))
{
students = students.Where(s => s.StudentName.Contains(search)
|| s.Gender.Contains(search));
}
return View(students.ToList());
}
我不知道您的要求,但是当您搜索数据到db时,您只需要这些数据中的一个或两个属性即可。
@using(Html.BeginForm("Index","Student",FormMethod.Get)){
<div id="search">
<input type="text" name="search" />
<input type="submit" name="submit" value="Search" class="btn btn-default"/>
</div>
}
答案 1 :(得分:-1)
创建POST。 GET用于请求数据。您正尝试发送回数据(搜索参数),而这需要回发。
[HttpGet]
public ActionResult SearchIndex()
{
return View();
}
[HttpPost,ActionName("Index")]
public ActionResult SearchIndex(string option, string search)
{
if (option == "Name")
{
var a = db.Students.Where(x => x.StudentName == search || search == null);
return View(a.ToList());
}
else if (option == "Gender")
{
return View(db.Students.Where(x => x.Gender == search).ToList());
}
else
{
return View(db.Students.Where(x => x.RegNo == search || search == null).ToList())
}
}
在使用中,还需要更新html以使用FormMethod.Post。
@using (Html.BeginForm("Index", "Student", FormMethod.Post, new { encType = "multipart/form-data" }))
编辑 再次考虑,我认为您只需要将multipart / form-data添加到html中。
@using (Html.BeginForm("Index", "Student", FormMethod.Get, new { encType = "multipart/form-data" }))