我已经陷入这个问题好几个小时了。当我点击提交按钮时,它没有响应,只是刷新页面。
索引控制器
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>
}
能做些什么来解决这个问题?
我认为这段代码可以给出更多的结果
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());
}
我不知道你的要求,但当你搜索数据库中的数据时,你只需要从这些数据中获得一两个属性。
@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>
}
创建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。张贴在使用中。
@using (Html.BeginForm("Index", "Student", FormMethod.Post, new { encType = "multipart/form-data" }))
EDIT仔细想想,我认为您只需要将多部分/表单数据添加到html中。
@using (Html.BeginForm("Index", "Student", FormMethod.Get, new { encType = "multipart/form-data" }))