我正在使用Razorview和Entity Framework,我正在尝试实现以下目标:我有一个下拉列表(id = ColumnName),其中包含一个列名列表和一个文本框(id = SearchValue),用于从中搜索值表。单击按钮,我将从数据库中检索相关数据(列名称为' ColumnName' selected,value =' SearchValue')。这很好,但是当我收到相关数据后,我就失去了SearchValue'和' ColumnName'选择。我不确定如何保存选定和输入的值,只需返回表中的相关数据。我的代码如下:
HTML:
<select id='mySelector' name="ColumnName">
<option value="">Please Select</option>
<option value='Country'>Country</option>
<option value='Title'>Title</option>
<option value='State'>State</option>
</select>
<input type="text" id="cs" name="SearchValue">
<input type="button" value="Search" onclick="location.href='@Url.Action("FilterByColumn", "CountryController")?SearchValue=' + document.getElementById('cs').value + '&ColumnName=' +document.getElementById('mySelector').value" />
<table id='myTable'>
// values
</table>
CountryController:
public ActionResult FilterByColumn(String ColumnName, String SearchValue)
{
if (!String.IsNullOrEmpty(SearchValue))
{
List<Country> result = new List<Country>();
result = db.Countries.ToList();
result = db.Countries.Where(ColumnName + ".Contains" + "(\"" + SearchValue.ToLower() + "\")").ToList();
return View(result);
}
return RedirectToAction("Index");
}
注意:我还有其他方法,比如在此控制器中创建,编辑。
答案 0 :(得分:1)
我继续做了searchvalue razor示例,你只需要为列名做适当的剃刀元素,这将是一个选定的项目,再次在线提供大量的教程。
<form id="myform" action="FilterByColumn" method="post">
<select id='mySelector' name="ColumnName">
<option value="">Please Select</option>
<option value='Country'>Country</option>
<option value='Title'>Title</option>
<option value='State'>State</option>
</select>
@if (ViewBag.searchval != "" ){
@Html.Raw ('<input type='text' id='cs' name='SearchValue' value="+@viewbag.searchval+">");
}
else
{
@Html.Raw ('<input type="text" id="cs" name='SearchValue'>");
}
<input type="submit" value="Search" />
</form>
<table id='myTable'>
// values
</table>
CountryController:
public ActionResult FilterByColumn(String ColumnName)
{
ViewBag.searchval = "";
string SearchValue = Request.Form["SearchValue"];
if (SearchValue != "" )
{
List<Country> result = new List<Country>();
result = db.Countries.ToList();
result = db.Countries.Where(ColumnName + ".Contains" + "(\"" + SearchValue.ToLower() + "\")").ToList();
ViewBag.searchval = SearchValue;
return View(result);
}
return RedirectToAction("Index");
}