1

iam using mvc3,i have empty search form in that am using only textbox with search button,if user search for data and click search button,then it has to show the result which is taken from index view. Here is my controller action

     public ActionResult Search(string searchString)
    {
        var certificate = from s in db.certificate_mst
                       select s;
        if (!String.IsNullOrEmpty(searchString))
        {
            certificate = certificate.Where(s => s.CertificateNo.Contains(searchString));

        }
        return View(certificate);
    }

and my view code is

   @using (Html.BeginForm("Search","certificate1",FormMethod.Get))
{
<p><b>CertificateNo</b>:@Html.TextBox("searchString")
<input type="submit" value="search" />

By using this code,searching is working but am getting all the data which i have in my view before searching,i need to have empty form before click search button and only after i click search it has to show result.

4

2 回答 2

3

只需在检索证书之前检查空字符串

public ActionResult Search(string searchString)
{
    if (String.IsNullOrEmpty(searchString))
    {
         //Return empty viewModel
         return View();
    }

    var certificate = db.certificate_mst.Where(s => s.CertificateNo.Contains(searchString));
    return View(certificate);
}

还可以查看我写的关于 IQueryable 的搜索扩展方法的博客文章,它应该对你有所帮助

http://www.ninjanye.co.uk/2013/03/c-generic-search-extension-method-for.html

http://jnye.co/Posts/6/c%23-generic-search-extension-method-for-iqueryable

使用扩展方法,您的代码变为...

var certificate = db.certificate_mst.Search(s => s.CertificateNo, searchString));

首选方法是将视图拆分为 get 和 post 操作,如下所示。这允许您在发布空字符串时返回所有结果:

public ActionResult Search()
{
    return View();
}

[HttpPost]
public ActionResult Search(string searchString)
{
    var certificate = db.certificate_mst.Where(s => s.CertificateNo.Contains(searchString));
    //OR
    //var certificate = db.certificate_mst.Search(s => s.CertificateNo, searchString));
    return View(certificate);        
}
于 2013-07-19T11:49:03.493 回答
1

尝试为您的搜索屏幕使用视图模型,以防您稍后需要在搜索中添加更多过滤项。然后你不会有每个搜索过滤器的参数,只有一个,即你的视图模型。您现在拥有的方式现在也可以使用,下面只是一种替代方法。

视图模型可能如下所示:

public class SearchViewModel
{
     public string SearchString { get; set; }

     // Other filter items if you need anything else
}

您的控制器的操作方法

public ActionResult Search()
{
     SearchViewModel viewModel = new SearchViewModel();

     return View(viewModel);
}

从控制器中删除数据访问权限并通过服务层或存储库工作:

[HttpPost]
public ActionResult Search(SearchViewModel viewModel)
{
     // Check for null viewModel

     if (!ModelState.IsValid)
     {
          // A possible failed validation is when no search string was entered,
          // and then you don't want to do any database calls.
          // Just pass back the view model and let the view handle the displaying of errors

          return View(viewModel);
     }

     // If validation succeeds now you can use your search string to retrieve data
     searchService.Search(viewModel.SearchString);

     // Do what else you need to do and the return the correct view

     return View();
}

您的搜索视图可能如下所示:

@model YourProject.ViewModels.Searches.SearchViewModel

@using (Html.BeginForm())
{
     @Html.TextBoxFor(x => x.SearchString)
     @Html.ValidationMessageFor(x => x.SearchString)

     <button id="searchButton" type="submit">Search</button>
}

我希望这现在更有意义。

于 2013-07-19T12:44:32.770 回答