如何在asp.net MVC 5中的表单中显示搜索结果?

时间:2016-09-22 08:09:48

标签: c# asp.net asp.net-mvc asp.net-mvc-4

我是asp.net MVC模式的新手,我正在学习各种功能。一个功能是"搜索"。我的目标是在我输入搜索文本框中的值并按下回车按钮后,现在的结果会显示在一行中,但我还想填充文本框中的相应值(Text1,Text2,Text3)。我如何实现这一目标?

我的控制器被称为" HomeController"方法是索引。

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using SearchusingMVC.Models;

namespace SearchusingMVC.Controllers
{
    public class HomeController : Controller
    {
        private SampleDBContext db = new SampleDBContext();

        // GET: /Home/
        public ActionResult Index(string searchBy, string search)
        {
            if (searchBy == "Gender")
            {
                return View(db.tblEmployees.Where(x => x.Gender == search || search == null).ToList());
            }
            else
            {
                return View(db.tblEmployees.Where(x => x.Name.StartsWith(search) || search == null).ToList());
            }
        }

        // GET: /Home/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            tblEmployee tblemployee = db.tblEmployees.Find(id);
            if (tblemployee == null)
            {
                return HttpNotFound();
            }
            return View(tblemployee);
        }

        // GET: /Home/Create
        public ActionResult Create()
        {
            return View();
        }

        // POST: /Home/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include="ID,Name,Gender,Email")] tblEmployee tblemployee)
        {
            if (ModelState.IsValid)
            {
                db.tblEmployees.Add(tblemployee);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(tblemployee);
        }

        // GET: /Home/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            tblEmployee tblemployee = db.tblEmployees.Find(id);
            if (tblemployee == null)
            {
                return HttpNotFound();
            }
            return View(tblemployee);
        }

        // POST: /Home/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include="ID,Name,Gender,Email")] tblEmployee tblemployee)
        {
            if (ModelState.IsValid)
            {
                db.Entry(tblemployee).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(tblemployee);
        }

        // GET: /Home/Delete/5
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            tblEmployee tblemployee = db.tblEmployees.Find(id);
            if (tblemployee == null)
            {
                return HttpNotFound();
            }
            return View(tblemployee);
        }

        // POST: /Home/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            tblEmployee tblemployee = db.tblEmployees.Find(id);
            db.tblEmployees.Remove(tblemployee);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}

该视图称为Index.cshtml

@model IEnumerable<SearchusingMVC.Models.tblEmployee>

@{
    ViewBag.Title = "Index";
}



<h2>Index</h2>

<p>
    @using (@Html.BeginForm("Index", "Home", FormMethod.Get))
    {
        <b>Search By:</b>
        @Html.RadioButton("searchBy", "Name", true) <text>Name</text>
        @Html.RadioButton("searchBy", "Gender") <text>Gender</text><br />
        @Html.TextBox("search") <input type="submit" value="search" />
    }
</p>


<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Name)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Gender)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Email)
        </th>
        <th></th>
    </tr>
@if (Model.Count() == 0)
{
    <tr>
        <td colspan="4">
            No records match search criteria
        </td>
    </tr>
}
    else
    {
foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Name)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Gender)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Email)
        </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>






 <label for="Name">Name:</label>
&nbsp &nbsp
<input id="Text1" type="text" />
<br />
<br />
 <label for="Gender">Gender:</label>
&nbsp

<input id="Text2" type="text" />
<br />
<br />
 <label for="Email">Email:</label>
&nbsp&nbsp&nbsp&nbsp

<input id="Text3" type="text" />
<br />
<br /

1 个答案:

答案 0 :(得分:0)

将您的搜索字词放在ViewBag

    // GET: /Home/
    public ActionResult Index(string searchBy, string search)
    {
        ViewBag.SearchBy = searchBy;
        ViewBag.Search = search;
        if (searchBy == "Gender")
        {
            return View(db.tblEmployees.Where(x => x.Gender == search || search == null).ToList());
        }
        else
        {
            return View(db.tblEmployees.Where(x => x.Name.StartsWith(search) || search == null).ToList());
        }
    }

然后在视图中使用它们:

<label for="Name">Name:</label>
&nbsp &nbsp
<input id="Text1" type="text" value="@ViewBag.Search" />
<br />
<br />
 <label for="Gender">Gender:</label>
&nbsp

<input id="Text2" type="text" />
<br />
<br />

当然这是一个例子。您需要编写一些javascript来正确填充搜索字段。这是因为您使用的是“searchBy”字段。

然而,最好在搜索中为每个字段指定一个参数,并省略“searchBy”参数