我正在尝试搜索学生并根据在文本框中输入的值显示学生记录。我不知道如何在不使用Jquery的情况下传递值。有办法吗?
控制器:
[Authorize]
public ActionResult GetStudentsByName(string name)
{
SchoolDbEntities db = new SchoolDbEntities();
var student = db.Students.Where(s => s.StudentName.Contains(name));
return View(student);
}
查看
<div class="panel-body">
<div class="form-group">
@Html.LabelFor(m=> m.SearchEntity.StudentName,"Search Student")
@Html.TextBoxFor(m => m.SearchEntity.StudentName, new { @class = "form-control" })
</div>
</div>
<div class="panel-footer">
<button type="button" onclick="location.href='@Url.Action("GetStudentsByName", "Student")'" class="btn btn-primary">Search <i class="glyphicon glyphicon-search"></i></button>
<button id="Reset"
class="btn btn-primary btn-sm">
<i class="glyphicon glyphicon-share-alt"></i> Reset
</button>
</div>
我需要传递参数,在@ Url.Action(“GetStudentsByName”,“学生”,新的{@ name ='值来自文本框')
它也是一个获取方法,而不是httpPost。
答案 0 :(得分:0)
您可以将输入表单元素保留在表单控件中,并通过GET方法提交表单。
确保输入元素名称与操作方法参数名称
匹配@using (Html.BeginForm("GetStudentsByName","Student",FormMethod.Get))
{
<input type="text" name="name"/>
<button type="submit" class="btn btn-primary">Search </button>
}
答案 1 :(得分:0)
如果你不想让按钮成为提交按钮,那么你可以给你的文本框一个特定的课程。
<div class="panel-body">
<div class="form-group">
@Html.LabelFor(m=> m.SearchEntity.StudentName,"Search Student")
@Html.TextBoxFor(m => m.SearchEntity.StudentName, new { @class = "form-control,txt-input" })
</div>
</div>
<div class="panel-footer">
<button type="button" onclick="location.href='@Url.Action("GetStudentsByName", "Student",new { data= "STUDENT_NAME" })'.replace("STUDENT_NAME", $('.txt-input').val())" class="btn btn-primary">Search <i class="glyphicon glyphicon-search"></i></button>
<button id="Reset"
class="btn btn-primary btn-sm">
<i class="glyphicon glyphicon-share-alt"></i> Reset
</button>
</div>
我尝试了这样的事情很久以后,我认为它应该有效。
答案 2 :(得分:0)
试试这个
查看
@using (Html.BeginForm("GetStudentsByName", "Home"))
{
<div class="form-group">
@Html.LabelFor(m => m.StudentName, "Search Student")
@Html.TextBoxFor(m => m.StudentName, new { @class = "form-control" })
</div>
<input type="submit" value="Search" class="btn btn-primary" />
}
</div>
控制器
public ActionResult GetStudentsByName()
{
string name = Request.Form["StudentName"]
SchoolDbEntities db = new SchoolDbEntities();
var student = db.Students.Where(s => s.StudentName.Contains(name));
return View(student);
}
使用Request.Form["StudentName"]
,您可以访问从视图到控制器的值。另外,还有一个建议,使用using
语句来帮助管理未处理的内存。例如
public ActionResult GetStudentsByName()
{
string name = Request.Form["StudentName"]
using(SchoolDbEntities db = new SchoolDbEntities())
{
var student = db.Students.Where(s => s.StudentName.Contains(name));
return View(student);
}
}