索引页面上的搜索功能

时间:2016-02-18 04:14:40

标签: search

我有3个搜索字段的代码。一个是RadioButton,两个是来自数据库的DropdownList

我必须根据上述标准进行搜索,但我会得到错误的结果。

public ActionResult Index(string searchby,string did, string sid)
{
    ViewBag.did = new SelectList(db.destcities, "Id", "name",did);
    ViewBag.sid = new SelectList(db.sourcecities, "Id", "name",sid);
    var searches = db.searches.Include(s => s.destcity)
                              .Include(s => s.sourcecity);
    If(!String.IsNullOrEmpty(searchby))
    {
        searches= db.searches.Where(a => a.type == searchby);
    }
    if(!String.IsNullOrEmpty(did))
    {
        int q = int.Parse(did);
        searches= db.searches.Where(s => s.destcity.Id == q);
    }
    if (!String.IsNullOrEmpty(sid))
    {
        int p = int.Parse(sid);
        searches= db.searches.Where(c => c.sourcecity.Id == p);
    }
    return View(searches);
}

并且查看代码是

@model IEnumerable<Web.Models.search>

@{
    ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>@Html.ActionLink("Create New", "Create") </p>
@using (Html.BeginForm("Index","searches",FormMethod.Get))
{
    <label>Trip type</label>
    @Html.RadioButton("searchby", "Oneway")<text>Oneway</text>
    @Html.RadioButton("searchby", "Roundtrip")<text>Roundtrip</text>
    @Html.DropDownList("did", null, htmlAttributes: new { @class = "form-control" })
    @Html.DropDownList("sid", null, htmlAttributes: new { @class = "form-control" })
    <input type="submit" value="Create" class="btn btn-default" />
}
<table class="table">
    <tr>
        <th>@Html.DisplayNameFor(model => model.destcity.name)</th>
        <th>@Html.DisplayNameFor(model => model.sourcecity.name)</th>
        <th>@Html.DisplayNameFor(model => model.vehicle)</th>
        <th>@Html.DisplayNameFor(model => model.km)</th>
        <th>@Html.DisplayNameFor(model => model.price)</th>
        <th>@Html.DisplayNameFor(model => model.type)</th>
        <th></th>
    </tr>
    @foreach (var item in Model)
    {
        <tr>
            <td>@Html.DisplayFor(modelItem => item.destcity.name)</td>
            <td>@Html.DisplayFor(modelItem => item.sourcecity.name)</td>
            <td>@Html.DisplayFor(modelItem => item.vehicle)</td>
            <td>@Html.DisplayFor(modelItem => item.km)</td>
            <td>@Html.DisplayFor(modelItem => item.price)</td>
            <td>@Html.DisplayFor(modelItem => item.type)</td>
            <td>@Html.ActionLink("Edit", "Edit", new { id=item.Id })
                | @Html.ActionLink("Details", "Details", new { id=item.Id })
                | @Html.ActionLink("Delete", "Delete", new { id=item.Id })
            </td>
        </tr>
    }
</table>

0 个答案:

没有答案