C# 如何在asp.net mvc3中进行搜索

C# 如何在asp.net mvc3中进行搜索,c#,asp.net,asp.net-mvc,asp.net-mvc-3,C#,Asp.net,Asp.net Mvc,Asp.net Mvc 3,我使用mvc3,我有空的搜索表单,我只使用带有搜索按钮的文本框,如果用户搜索数据并单击搜索按钮,那么它必须显示从索引视图中获取的结果。 这是我的控制器动作 public ActionResult Search(string searchString) { var certificate = from s in db.certificate_mst select s; if (!String.IsNul

我使用mvc3,我有空的搜索表单,我只使用带有搜索按钮的文本框,如果用户搜索数据并单击搜索按钮,那么它必须显示从索引视图中获取的结果。 这是我的控制器动作

     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);
    }
我的视图代码是

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

通过使用此代码,搜索工作正常,但我在搜索之前获取了视图中的所有数据,在单击“搜索”按钮之前,我需要有空表单,只有在单击“搜索”之后,它才能显示结果。

检索证书之前,只需检查空字符串即可

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的搜索扩展方法的博文,这应该对您有所帮助

使用扩展方法,您的代码将变成

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);        
}

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

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的搜索扩展方法的博文,这应该对您有所帮助

使用扩展方法,您的代码将变成

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);        
}

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

视图模型可能如下所示:

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

     // Other filter items if you need anything else
}
@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>
}
控制器的操作方法

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();
}
您的搜索视图可以如下所示:

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

     // Other filter items if you need anything else
}
@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>
}

我希望这现在更有意义。

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

视图模型可能如下所示:

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

     // Other filter items if you need anything else
}
@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>
}
控制器的操作方法

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();
}
您的搜索视图可以如下所示:

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

     // Other filter items if you need anything else
}
@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>
}

我希望这现在更有意义。

谢谢你宝贵的回复,这对我很有帮助。我需要另一个帮助,我有一个专栏,其中我需要根据一些标准自动生成id值,但不需要标识,你能帮我使用示例codeHi@sanjay1234遵循哪种方法吗,你的搜索请求有点模棱两可,我无法给你一个有意义的答案。我建议你提出一个尽可能详细的新问题,我会留意的。如果您认为此答案已经回答了您的问题,请接受此答案谢谢您宝贵的回答,这对我非常有帮助。我需要其他帮助,我有一个专栏,其中我需要根据一些标准自动生成id值,但不需要标识,您能帮我使用示例codeHi@sanjay1234遵循哪种方法吗,你的搜索请求有点模棱两可,我无法给你一个有意义的答案。我建议你提出一个尽可能详细的新问题,我会留意的。如果您认为此答案已回答您的问题,请接受此答案