简体   繁体   中英

how to solve this error of Search?

I have a problem with my Search here is my code :

Controller

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

View

@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"] }))

my problem is, when i search using Fname or lname it does not display but when i search using H_no it is displaying, please help.

On running the bare bones of your code I can see that it is getting the correct values for findby what I did not use in test app is this portion;

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"] }))

below is a screen grab where it successfully hits and gets the correct findby value. Can you check your query if that is the problem? 在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM