码迷,mamicode.com
首页 > Web开发 > 详细

MVC3.0搜索实例

时间:2015-09-04 07:10:51      阅读:220      评论:0      收藏:0      [点我收藏+]

标签:

   今天是举行大阅兵的日子,为庆祝抗日战争胜利70周年。为祖国的繁荣昌盛感到无比的自豪和骄傲,祖国万岁。

    在msdn上看了大半天的的 getting-started-with-aspnet-mvc3。感觉官网的教程就是不一样。图文并茂,精简,且可以和作者探讨问题在评论那,看到Examining the Edit       Methods and Edit View (C#)这里的时候有了些新的实践。决定结合那个mvc demo继续练习。在@Recluse_Xpy的帮助下实现了我要的扩展搜索功能。和他聊了下感觉自己进     步空间很是广阔。要有学习和自我学习的意识,怕的是比自己优秀的人更加努力,你没有不努力的理由。感谢他热心的远程调试。共勉。

    第一步、 在控制器中写了个搜索的方法,主要是显示学生的信息,通过姓名和班级实现数据的,筛选控制器代码如下:

    

技术分享
public ViewResult SearchIndex(string classNames, string str)
        {
            var list = new List<string>();
            var csname_query = from c in db.scClassList
                               orderby c.className
                               select c.className;


            list.AddRange(csname_query.Distinct());
            ViewBag.classNames = new SelectList(list);

            var studentlist = db.stuList.Include(d => d.scClass).ToList();

            if (!String.IsNullOrEmpty(str))
            {
                studentlist = studentlist.Where(s => s.stuName.Contains(str)).ToList();

            }
            if (String.IsNullOrEmpty(classNames))
            {

                return View(studentlist);

            }
            else
            {
                return View(studentlist.Where(s => s.scClass.className == classNames));

            }
           


        }
View Code

 

  第二部、生成对应的视图、并做相应的修改见代码:

技术分享
@model IEnumerable<Mvc3Demo.Models.Student>
@{
    ViewBag.Title = "SearchIndex";
}
<h2>
    SearchIndex</h2>
<p>
    @Html.ActionLink("Create New", "Create")
    @using (Html.BeginForm("SearchIndex", "StudentManager", FormMethod.Get))
    {
        <p>
            ClassName:@Html.DropDownList("classNames", "All")
            Name:@Html.TextBox("str")<br />
            <input type="submit" value="Filter" />
        </p>
    }
</p>
<h2>
    SearchIndex</h2>
<table>
    <tr>
        <th>
            stuName
        </th>
        <th>
            stuSex
        </th>
        <th>
            age
        </th>
        <th>
            scClassID
        </th>
        <th>
        </th>
    </tr>
    @foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.stuName)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.stuSex)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.age)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.scClass.className)
            </td>
            <td>
                @Html.ActionLink("Edit", "Edit", new { id = item.stuid }) |
                @Html.ActionLink("Details", "Details", new { id = item.stuid }) |
                @Html.ActionLink("Delete", "Delete", new { id = item.stuid })
            </td>
        </tr>
    }
</table>
View Code

 

  效果图如下:通过输入Name文本框对应的值搜索对应的学生,通过班级可以查出对应的学生:【列表=》名称搜索结果=》班级搜索结果】

技术分享    技术分享   技术分享

 

MVC3.0搜索实例

标签:

原文地址:http://www.cnblogs.com/professional-NET/p/4781034.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!