AspNet MVC4 教学-19:Asp.Net MVC4 利用Linq技术的搜索应用快速Demo

A.创建mvc4项目,Basic类型.

B.创建Model:

Student.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MvcSearchTest.Models
{
    public class Student
    {
        public int ID { set; get; }
        public string Name { set; get; }
        public int Age { set; get; }
        public bool Male { set; get; }
    }

}

C.创建控制器:

HomeController.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcSearchTest.Models;

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

        // GET: /Home/

        public ActionResult Index()
        {
            return View();
        }
        public ActionResult Search(string Name,int? Age)
        {  
            int age ;
            age = Age??-1;
            if (string.IsNullOrEmpty(Name) != true && string.IsNullOrWhiteSpace(Name) != true)
            {

                //linq方式,查询所有影片,仅定义,不执行
                //var students = from m in db.Students
                //               select m;
                var students = db.Students;
                if (age == -1)
                {
                    var results = students.Where(m => m.Name == Name).OrderByDescending(m => m.Age).ThenBy(m=>m.Name);
                    return View(results.ToList());
                }
                else
                {
                    var results = students.Where(m => m.Name == Name).Where(m=>m.Age==age).OrderBy(m => m.Age);
                    return View(results.ToList());
                }
            }
 
            ViewBag.Tip = "Name不能为空。";
            return View("Index");
        }
    }
}

StudentController.cs:

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

namespace MvcSearchTest.Controllers
{
    public class StudentController : Controller
    {
        private MvcSearchTestContext db = new MvcSearchTestContext();

        //
        // GET: /Student/

        public ActionResult Index()
        {
            return View(db.Students.ToList());
        }

        //
        // GET: /Student/Details/5

        public ActionResult Details(int id = 0)
        {
            Student student = db.Students.Find(id);
            if (student == null)
            {
                return HttpNotFound();
            }
            return View(student);
        }

        //
        // GET: /Student/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Student/Create

        [HttpPost]
        public ActionResult Create(Student student)
        {
            if (ModelState.IsValid)
            {
                db.Students.Add(student);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(student);
        }

        //
        // GET: /Student/Edit/5

        public ActionResult Edit(int id = 0)
        {
            Student student = db.Students.Find(id);
            if (student == null)
            {
                return HttpNotFound();
            }
            return View(student);
        }

        //
        // POST: /Student/Edit/5

        [HttpPost]
        public ActionResult Edit(Student student)
        {
            if (ModelState.IsValid)
            {
                db.Entry(student).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(student);
        }

        //
        // GET: /Student/Delete/5

        public ActionResult Delete(int id = 0)
        {
            Student student = db.Students.Find(id);
            if (student == null)
            {
                return HttpNotFound();
            }
            return View(student);
        }

        //
        // POST: /Student/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Student student = db.Students.Find(id);
            db.Students.Remove(student);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

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

D.创建相应的View:

Home/Index.cshtml:

@{
    ViewBag.Title = "Index";
}

<h2>Search Demo</h2>
<h2>@ViewBag.Tip</h2>
@using (Html.BeginForm("Search","Home"))
{ 
<p>Name: @Html.TextBox("Name")<br /> </p>
<p>Age: @Html.TextBox("Age")<br /> </p> 
<input type="submit" value="查询" />
}
<p>@Html.ActionLink("学生管理", "Index", "Student")</p>

Home/Search.cshtml:

@model IEnumerable<MvcSearchTest.Models.Student>

@{
    ViewBag.Title = "Search";
}

<h2>Search Result</h2>

<table>
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Name)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Age)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Male)
        </th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Name)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Age)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Male)
        </td>     
    </tr>
}

</table>

提示:

1.利用Student类及平台的内置List模板产生Search.chhtml文件,

2.,利用Student类及Entity Framework相关技术创建StudentController.cs文件,并且产生数据库上下文及相应的数据库,再利用平台相关技术产生Student类相应的View文件.Index,Edit,Create,Delete,Detail等,如下图所示:




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

gCodeTop 格码拓普 老师

您的鼓励.我的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值