Searching - from index view and result to another view

330 Views Asked by At

i'm creating a search function in mvc5 my program works like this: Index view have search box and button and the result of that also displays in index view

so my problem is that how do i display the result in another view - say like searchresult.cshtml and not in index view?

here's my controller:

public ActionResult Index(string searching)
{
 return View(db.TblId.Where(x => x.IdNumber.Contains(searching) || searching == null));
}

my index view (i just removed the other text contents, only included the search result)

@model IEnumerable<MVC5_Search.Models.TblId>

@using (Html.BeginForm("Index", "Home", FormMethod.Get))
{
    @Html.TextBox("searching")<input type="submit" value="Search" />
}

<table>
    <thead>
        <tr>
            <td>Id Number</td>
            <td>First Name</td>
            <td>Middle Name</td>
            <td>Last Name</td>
        </tr>
    </thead>
    <tbody>
        @if (Model.Count() == 0)
        {
            <tr>
                <td colspan="3" style="color: red">
                    No Result!
                </td>
            </tr>
        }
        else
        {
            foreach (var item in Model)
            {
                <tr>
                    <td>@item.IdNumber</td>
                    <td>@item.Firstname</td>
                    <td>@item.Middlename</td>
                    <td>@item.Lastname</td>
                </tr>
            }
        }
    </tbody>
</table>

i'm using this together with entity framework

EDIT:(trying to solve)

here's what i've done so far,

i created another controller (SearchingController) to avoid conflict with the main controller,

[HttpGet]
        public ViewResult SearchResult(string searching)
        {
            return View("SearchResult", db.TblId.Where(x => x.TId.Contains(searching) || searching == null));
        }

and then the view SearchResult.cshtml

@model IEnumerable<TblId.Models.TId>

@{
    ViewBag.Title = "searchresult";
}

<table>
    <thead>
        <tr>
            <td>IdNumber</td>
            <td>First Name</td>
            <td>Middle Name</td>
            <td>Last Name</td>
        </tr>
    </thead>
    <tbody>
        @if (Model.Count() == 0)
        {
            <tr>
                <td colspan="3" style="color: red">
                    No Result!
                </td>
            </tr>
        }
        else
        {
            foreach (var item in Model)
            {
                <tr>
                    <td>@item.IdNumber</td>
                    <td>@item.Firstname</td>
                    <td>@item.Middlename</td>
                    <td>@item.Lastname</td>
                </tr>
            }
        }
    </tbody>
</table>

and in my index view,

@using (Html.BeginForm("SearchResult", "Searching", FormMethod.Get))
                            {
                                @*@Html.TextBox("searching")*@
                                <input type="text" id="searching" name="searching" />

                                <button type="submit" name="searching" id="searching "class="btn btn-secondary">
                                    Verify
                                    <br>
                                </button>
                            }

still not working as expected

when i clicked search button, it just change the url to something like this

/Index?searching=T101&searching=

T101 - is the Id i'm searching

1

There are 1 best solutions below

6
PNDev On BEST ANSWER

Follow these steps:

-> Create a new view searchresult.cshtml with below contents

@model IEnumerable<MVC5_Search.Models.TblId>

<table>
    <thead>
        <tr>
            <td>Id Number</td>
            <td>First Name</td>
            <td>Middle Name</td>
            <td>Last Name</td>
        </tr>
    </thead>
    <tbody>
        @if (Model.Count() == 0)
        {
            <tr>
                <td colspan="3" style="color: red">
                    No Result!
                </td>
            </tr>
        }
        else
        {
            foreach (var item in Model)
            {
                <tr>
                    <td>@item.IdNumber</td>
                    <td>@item.Firstname</td>
                    <td>@item.Middlename</td>
                    <td>@item.Lastname</td>
                </tr>
            }
        }
    </tbody>
</table>

-> Modify your index post action to

[HttpPost]    
public ViewResult Index(string searching)
    {
     return View("searchresult",db.TblId.Where(x => x.IdNumber.Contains(searching) || searching == null));
    }