当前在使用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();
}
}
}
}

MVC 构造新Model实现内容搜索的更多相关文章

  1. GBDT原理及利用GBDT构造新的特征-Python实现

    1. 背景 1.1 Gradient Boosting Gradient Boosting是一种Boosting的方法,它主要的思想是,每一次建立模型是在之前建立模型损失函数的梯度下降方向.损失函数是 ...

  2. Redis 实战 —— 10. 实现内容搜索、定向广告和职位搜索

    使用 Redis 进行搜索 P153 通过改变程序搜索数据的方式,并使用 Redis 来减少绝大部分基于单词或者关键字进行的内容搜索操作的执行时间. P154 基本搜索原理 P154 倒排索引 (in ...

  3. .net mvc web api 返回 json 内容,过滤值为null的属性

    原文:http://blog.csdn.net/xxj_jing/article/details/49508557 版权声明:本文为博主原创文章,未经博主允许不得转载. .net mvc web ap ...

  4. Asp.net MVC 4新项目中创建area的后续操作

    Asp.net MVC 4新项目中创建area后,往往HomeController与area的HomeController路由发生混淆,需要手工设置一些地方避免mvc无法识别默认路由的状况. 无废话具 ...

  5. MVC模式(Model View Controller)下实现数据库的连接,对数据的删,查操作

    MVC模式(Model View Controller): Model:DAO模型 View:JSP  在页面上填写java代码实现显示 Controller:Servlet 重定向和请求的转发: 若 ...

  6. 利用GBDT模型构造新特征具体方法

    利用GBDT模型构造新特征具体方法 数据挖掘入门与实战  公众号: datadw   实际问题中,可直接用于机器学**模型的特征往往并不多.能否从"混乱"的原始log中挖掘到有用的 ...

  7. ASP.NET MVC中默认Model Binder绑定Action参数为List、Dictionary等集合的实例

    在实际的ASP.NET mvc项目开发中,有时会遇到一个参数是一个List.Dictionary等集合类型的情况,默认的情况ASP.NET MVC框架是怎么为我们绑定ASP.NET MVC的Actio ...

  8. Matrix Power Series(POJ 3233 构造新矩阵求解+ 快速矩阵幂)

    题目大意:给定A,k,m(取模),求解S = A + A2 + A3 + … + Ak. 思路:此题为求解幂的和,一开始直接一个个乘,TLE.时间消耗在累加上.此处巧妙构造新矩阵 p=    A 0 ...

  9. ASP.NET MVC中对Model进行分步验证的解决方法

    原文:ASP.NET MVC中对Model进行分步验证的解决方法 在我之前的文章:ASP.NET MVC2.0结合WF4.0实现用户多步注册流程中将一个用户的注册分成了四步,而这四个步骤都是在完善一个 ...

随机推荐

  1. spring使用jackson返回object报错:Handler execution resulted in exception: Could not find acceptable representation

    问题:在springmvc中添加Jackson jar包返回Object类型,处理器方法的produces属性不写,默认根据类型,但如果指定了(错误原因)produces = "text/h ...

  2. 5月18日 HTML 个人简历

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  3. Help Me Escape (ZOJ 3640)

    J - Help Me Escape Crawling in process... Crawling failed Time Limit:2000MS     Memory Limit:32768KB ...

  4. cookie、 sessionStorage 、localStorage之间的区别和使用

    1.cookie:存储在用户本地终端上的数据.有时也用cookies,指某些网站为了辨别用户身份,进行session跟踪而存储在本地终端上的数据,通常经过加密.一般应用最典型的案列就是判断注册用户是否 ...

  5. linux shell 单引号 双引号 反引号的区别

    一.单引号和双引号 首先, 单引号和双引号,都是为了解决中间有空格的问题. 因为空格在linux中时作为一个很典型的分隔符,比如 string1=this is a string,这样执行就会报错.为 ...

  6. linux 系统安装 mysql

    安装mysql所需要的依赖环境 yum -y install gcc gcc-c++ gcc-g77 autoconf automake zlib*  libxml* ncurses-devel li ...

  7. 所思所想 关于asp.net界面业务分离

    1.体会:使用ASP.NET控件来做前段是有很大的局限性的 2.使用拼接HTML的方式来写代码虽然不符合模式,但是有很大的灵活性 3.如果使用拼接字符串的方式来生成前台的代码,使用NV的话完全可以实现 ...

  8. 如何打开asp.net中的出错提示?在程序发布后

    答案:修改其中的一个配置节点,<customErrors mode="On"/>

  9. 部署步骤“回收 IIS 应用程序池”中出现错误: <nativehr>0x80070005</nativehr><nativestack></nativestack>拒绝访问。

    解决方法:以sharepoint管理员身份进入主站点,修改站点的网站集管理员.

  10. 闭包 (循环事件获取不到i) 和 各种解决循环获取不到i的解决方法

    for(var i in fav){ (function(){                var p=i;                var obj=$S.getId(fav[i]);     ...