如何解决这个搜索错误?

时间:2014-10-30 08:58:45

标签: c# asp.net-mvc

  

我的搜索问题在这里是我的代码:

控制器

public ActionResult Index(string Seach_Data, string findby, int? page, string currentFilter )
{
    if (findby == "fname")
    {
        string query = "SELECT v.h_no as h_no, v.h_initials , v.h_id as h_id , t.titles as titles, p.Suburb as Suburb, p.flatNo as flatNo, p.flatName as flatName,v.Email as Email, v.cell as cell,  v.tel_h as tel_h,  v.fname as fname, p.strname as strname, p.strNo as strNo, p.City as City, LEFT(v.lname, 255) as lname from [dbo].[Heads] v, [dbo].[Addresses1] p, [dbo].[Titles] t WHERE v.[h_ID] = p.[h_id] and v.[title_id]= t.[title_id] ";

        var ViewModel = db.Database.SqlQuery<HeadsViewModel>(query);
        return View(ViewModel.Where(x => x.fname == Seach_Data).ToList().ToPagedList(page ?? 1, 6));
    }
    else if (findby == "lname")
    {
        string query = "SELECT v.h_no as h_no, v.h_initials , v.h_id as h_id , t.titles as titles, p.Suburb as Suburb, p.flatNo as flatNo, p.flatName as flatName,v.Email as Email, v.cell as cell,  v.tel_h as tel_h,  v.fname as fname, p.strname as strname, p.strNo as strNo, p.City as City, LEFT(v.lname, 255) as lname from [dbo].[Heads] v, [dbo].[Addresses1] p, [dbo].[Titles] t WHERE v.[h_ID] = p.[h_id] and v.[title_id]= t.[title_id] ";
        var ViewModel = db.Database.SqlQuery<HeadsViewModel>(query);

        return View(ViewModel.Where(x => x.lname == Seach_Data).ToList().ToPagedList(page ?? 1, 6));  
    }
    else if (findby == "h_no")
    {
        string query = "SELECT v.h_no as h_no, v.h_initials , v.h_id as h_id , t.titles as titles, p.Suburb as Suburb, p.flatNo as flatNo, p.flatName as flatName,v.Email as Email, v.cell as cell,  v.tel_h as tel_h,  v.fname as fname, p.strname as strname, p.strNo as strNo, p.City as City, LEFT(v.lname, 255) as lname from [dbo].[Heads] v, [dbo].[Addresses1] p, [dbo].[Titles] t WHERE v.[h_ID] = p.[h_id] and v.[title_id]= t.[title_id] ";
        var ViewModel = db.Database.SqlQuery<HeadsViewModel>(query);
        return View(ViewModel.Where(x => x.h_no == Seach_Data).ToList().ToPagedList(page ?? 1, 6));
    }
    else
    {
        string query = "SELECT v.h_no as h_no, v.h_initials , v.h_id as h_id , t.titles as titles, p.Suburb as Suburb, p.flatNo as flatNo, p.flatName as flatName,v.Email as Email, v.cell as cell,  v.tel_h as tel_h,  v.fname as fname, p.strname as strname, p.strNo as strNo, p.City as City, LEFT(v.lname, 255) as lname from [dbo].[Heads] v, [dbo].[Addresses1] p, [dbo].[Titles] t WHERE v.[h_ID] = p.[h_id] and v.[title_id]= t.[title_id] ";
        var ViewModel = db.Database.SqlQuery<HeadsViewModel>(query);
        return View(ViewModel.ToList().ToPagedList( page ?? 1,6));
    }
}

查看

@using (Html.BeginForm("Index", "Heads", FormMethod.Get))
{
    <p>
        <b> Find by name:</b>@Html.RadioButton("findBy", "fname") <text>First Name</text>
        @Html.RadioButton("findBy", "lname")<text>Last Name</text><br />
        @Html.RadioButton("findBy", "h_no")<text>Header Number</text><br />
        @Html.TextBox("Seach_Data", ViewBag.FilterValue as string, ViewBag.CurrentFilter as string)
        <input type="submit" value="Find" />
    </p>
}


Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of @Model.PageCount

@Html.PagedListPager(Model, page => Url.Action("Index", new { page, findby = Request.QueryString["findby"], Seach_Data = Request.QueryString["Seach_Data"] }))

我的问题是,当我使用Fname或lname进行搜索时,它不显示但是当我使用H_no进行搜索时,它正在显示,请帮助。

1 个答案:

答案 0 :(得分:0)

在运行代码的骨架时,我可以看到它正在获取正确的值,因为我在测试应用程序中没有使用的是这部分;

Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of @Model.PageCount

@Html.PagedListPager(Model, page => Url.Action("Index", new { page, findby = Request.QueryString["findby"], Seach_Data = Request.QueryString["Seach_Data"] }))

下面是一个屏幕抓取,它成功点击并获得正确的findby值。如果这是问题,您能检查一下您的查询吗? enter image description here