2017-10-04 77 views
-1

我正在使用Razorview和实体框架,并试图实现以下功能:我有一个下拉列表(id = ColumnName),它具有列名称列表和textbox(id = SearchValue)用于从表中搜索的值。点击一个按钮时,我从数据库中检索相关数据(列名称为'ColumnName'且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

我做的方式是使用jQuery插入搜索值到一个隐藏字段,然后确保我传递回用ViewBag的观点。这将信息保存在URL之外,这可能会在稍后将数据库暴露给注入。 – Danimal

+0

此外,您可以使用ajax仅刷新要刷新的数据,并保留所有先前的页面数据。 – Danimal

+0

@Danimal:你能用一个例子来解释吗? – user7221204

回答

1

我继续做搜索值剃须刀的例子,你只需要为columname做一个适当的剃须刀元素,这将是一个选定的项目,再次是大量的在线教程。

<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="[email protected]+">"); 
} 
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"); 
     } 
+0

感谢您的支持回复。但是我的字符串SearchValue = Request.Form [“SearchValue”];即使我正在搜索框中输入内容,也会得到空值。 – user7221204

+0

我假设你使用了表单标签吧? – Danimal

+0

没有帖子的方法没有发布,我假设你在上面查看你的代码后使用了form标签我意识到你没有将它们添加到表单项上下,如果你的页面不在CountryController中,那么action可能会有所不同你可能需要做action =“CountryController/FilterByColumn – Danimal