当前位置: 首页 > news >正文

MVC 构造新Model实现内容搜索

当前在使用MVC开发一个网站,习惯了使用ASP.NET中控件,转到MVC之后突然对于页面和后台代码的传值感觉无从下手。花了点时间在网上看了写帖子后,想到了一个方法,重新构造一个新Model, 然后利用Model在页面和代码之间传值。

举例来说:

我数据库中有张学生表(Student),表中字段分别为:ID, StudentNo, StudentName, Gender, Birth, Domicile, Phone, Mail。相应的我的代码中有个名为Student的Model, 我想实现根据学生的学号,姓名,性别等条件从数据库中查询出符合条件的结果集。并将结果集在View中显示。

1。在View中提供输入框,使用户能够输入查询条件

2。在View中提供提交按钮,用户输入查询条件以后,点击提交按钮获得查询结果

3。难题出现了,如何将用户输入的查询条件传到Controler 代码中,并将查询到的结果返回到View中?

方法:

创建一个新Model,新Model比如取名叫Search,它包含两个部分,第一部分是查询条件(StuNo, StuName, Gender, ...),第二部分是查询结果(List<Student> StuResults).

创建基于Search类型的View,把Search传到View中,当点击提交按钮时,将 Search Model传到Controller,在Controller中得到Search类的查询条件,根据条件查询符合条件的Student的结果,将结果赋值给Search类的StuResults。然后将Search重新传回View。

代码:

新建Model

namespace CertTitle.Models
{
    public class Search
    {
        #region Search by Condition
        public string EntryID { get; set; }
        public string Title { get; set; }
        public string CDM { get; set; }
        public string Owner { get; set; }
        #endregion end of search condition

        #region Search Results
        public List<ExamEntry> ResultList { get; set; }
        #endregion end of search result
    }
}


Controller

        public ActionResult Lookup()
        {
            Search result = new Search();
            result.EntryID = "";
            result.Title = "";
            result.CDM = "";
            result.Owner = "";
            result.ResultList = new List<ExamEntry>();
            return View(result);
        }

        [HttpPost]
        public ActionResult Lookup(Search search)
        {
            string exam = search.EntryID;
            string keywordsInTitle = search.Title;
            string CDM = search.CDM;
            string AssignedTo = search.Owner;
            var results = db.ExamEntries.Where(e => (e.ExamID == exam||e.Title.Contains (keywordsInTitle)||
                e.CDM ==CDM ||e.AssignedTo == AssignedTo)).ToList();
            if (results != null)
                search.ResultList = results;
            return View(search);
        }


View

@using CertTitle.Models
@model CertTitle.Models.Search

@{
    ViewBag.Title = "Lookup";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Find Titles</h2>

<div>
    @using (Html.BeginForm())
    {
        <fieldset>
            <legend>Type Exam# which you want to find</legend>
            <div>
                Exam# @Html.TextBoxFor(model => model.EntryID) | Title @Html.TextBoxFor(model =>model.Title) | 
                CDM @Html.TextBoxFor(model => model.CDM) | Assigned To @Html.TextBoxFor(model => model.Owner) |<input type="submit" value="Search" />
            </div>
            <div>
                
            </div>
        </fieldset>
    }
</div>

<table id="tbInfo">
    <thead>
        <tr>
            <th>EXAM#</th>
            <th>VER</th>
            <th>LAN</th>
            <th>PjM</th>
            <th>CDM</th>           
            <th>Status</th>
            <th>REQUIRE</th>
            <th>ACTUAL</th>
            <th>ISSUE1</th>
            <th>ISSUE2</th>
            <th></th>
        </tr>
    </thead>
    <tbody>
        @foreach (var item in Model.ResultList)
    {
            <tr>
                <td>@Html.ActionLink(@item.ExamID,"Detail",new {id = @item.ID})</td>
                <td>@item.Version</td>
                <td>@item.Language</td>
                <td>@item.PjM</td>
                <td>@item.CDM</td>               
                <td>@CertTitleHelper.TaskStatusConverter(@item.Status)</td>
                <td>@item.PlanFinishDate</td>
                <td>@item.ActualFinishDate</td>
                <td>
                    @Html.ActionLink(@item.IssueCount.ToString(),"ViewIssueByTitle","CertIssue",new {id = @item.ID},null)
                </td>
                <td>
                    @Html.ActionLink(@item.IssueCountToEDP.ToString(),"ViewIssueByTitle","CertIssue",new {id = @item.ID},null)
                </td>
                <td style="width: 150px">
                    @Html.ActionLink("Edit","Edit",new{id = @item.ID}) |
                @Html.ActionLink("Missed Issue", "MissIssue", "CertIssue", new { id = @item.ID}, null)
                </td>
            </tr>
            <tr>
                <td></td>
                <td colspan="4">@item.Title</td>
                 <td>@item.AssignedTo</td>
            </tr>
    }
    </tbody>
</table>

View中使用到了一个

CertTitleHelper.TaskStatusConverter(int? )的方法,此方法根据一个Int型的值转换成相应的字符串值,例如枚举的转换。

 枚举在数据库中显示为Int,在页面中需要显示其字符串。转换代码如下

namespace CertTitle.Models
{
    public class CertTitleHelper
    {
        public static string TaskStatusConverter(int? status)
        {
            switch (status)
            {
                case (int)TaskStatus.NotAssigned:
                    return TaskStatus.NotAssigned.ToString();
                case (int)TaskStatus.Issues:
                    return TaskStatus.Issues.ToString();
                case (int)TaskStatus.InProgress:
                    return TaskStatus.InProgress.ToString();
                case (int)TaskStatus.Complete:
                    return TaskStatus.Complete.ToString();
                default:
                    return TaskStatus.NotAssigned.ToString();
            }
        }
    }
}

 

 

 

转载于:https://www.cnblogs.com/qixue/p/3683253.html

相关文章:

  • Ruby:Net::HTTP
  • 不错的JS
  • 关于ADO.NET连接SQL一些方法
  • 设计模式--职责链(学习)
  • [.NET 即时通信SignalR] 认识SignalR (一)
  • php统计在线人数,精确的统计在线人数的办法
  • 世界货币符号大全
  • 《每日一博》——学习
  • WhyDX9:翻写D3D红龙书中的程序
  • hive行转多列LATERAL VIEW explode
  • POJ-1182-食物链 解题报告
  • 关于WCF开发 相应流程注意事项
  • asp.net mvc部署
  • [leetcode]_String to Integer (atoi)
  • 收到offer 7B
  • Codepen 每日精选(2018-3-25)
  • Making An Indicator With Pure CSS
  • Mocha测试初探
  • mysql 5.6 原生Online DDL解析
  • PAT A1092
  • PHP 程序员也能做的 Java 开发 30分钟使用 netty 轻松打造一个高性能 websocket 服务...
  • Travix是如何部署应用程序到Kubernetes上的
  • ubuntu 下nginx安装 并支持https协议
  • vue--为什么data属性必须是一个函数
  • Vue学习第二天
  • 机器学习 vs. 深度学习
  • 简单实现一个textarea自适应高度
  • 力扣(LeetCode)56
  • 猫头鹰的深夜翻译:Java 2D Graphics, 简单的仿射变换
  • 通信类
  • 微信小程序--------语音识别(前端自己也能玩)
  • 新书推荐|Windows黑客编程技术详解
  • media数据库操作,可以进行增删改查,实现回收站,隐私照片功能 SharedPreferences存储地址:
  • 回归生活:清理微信公众号
  • ​​​​​​​GitLab 之 GitLab-Runner 安装,配置与问题汇总
  • ​flutter 代码混淆
  • ​sqlite3 --- SQLite 数据库 DB-API 2.0 接口模块​
  • ​ssh免密码登录设置及问题总结
  • ​VRRP 虚拟路由冗余协议(华为)
  • #!/usr/bin/python与#!/usr/bin/env python的区别
  • #数学建模# 线性规划问题的Matlab求解
  • (3)(3.5) 遥测无线电区域条例
  • (HAL库版)freeRTOS移植STMF103
  • (Redis使用系列) SpringBoot 中对应2.0.x版本的Redis配置 一
  • (附源码)springboot建达集团公司平台 毕业设计 141538
  • ***php进行支付宝开发中return_url和notify_url的区别分析
  • **PHP二维数组遍历时同时赋值
  • .[hudsonL@cock.li].mkp勒索加密数据库完美恢复---惜分飞
  • .cfg\.dat\.mak(持续补充)
  • .net 4.0 A potentially dangerous Request.Form value was detected from the client 的解决方案
  • .NET LINQ 通常分 Syntax Query 和Syntax Method
  • .NET 依赖注入和配置系统
  • .NET 自定义中间件 判断是否存在 AllowAnonymousAttribute 特性 来判断是否需要身份验证
  • .net6 webapi log4net完整配置使用流程
  • .skip() 和 .only() 的使用