MVC [Control与View交互]
<1>
Home控制器
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication4.Models; namespace MvcApplication4.Controllers
{
public class HomeController : Controller
{
private salesEntities db = new salesEntities();//salesEntities是一个ADO.NET实体数据模型类 //
// GET: /Home/ public ActionResult Index()
{
return View(db.T_UserInfo.ToList());
} //
// GET: /Home/Details/5 public ActionResult Details(int id = 0)
{
T_UserInfo t_userinfo = db.T_UserInfo.Single(t => t.id == id);
if (t_userinfo == null)
{
return HttpNotFound();
}
return View(t_userinfo);
} //
// GET: /Home/Create public ActionResult Create()
{
return View();
} //
// POST: /Home/Create [HttpPost]
public ActionResult Create(T_UserInfo t_userinfo)
{
if (ModelState.IsValid)
{
db.T_UserInfo.AddObject(t_userinfo);
db.SaveChanges();
return RedirectToAction("Index");
} return View(t_userinfo);
} //
// GET: /Home/Edit/5 public ActionResult Edit(int id = 0)
{
T_UserInfo t_userinfo = db.T_UserInfo.Single(t => t.id == id);
if (t_userinfo == null)
{
return HttpNotFound();
}
return View(t_userinfo);
} //
// POST: /Home/Edit/5 [HttpPost]
public ActionResult Edit(T_UserInfo t_userinfo)
{
if (ModelState.IsValid)
{
db.T_UserInfo.Attach(t_userinfo);
db.ObjectStateManager.ChangeObjectState(t_userinfo, EntityState.Modified);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(t_userinfo);
} //
// GET: /Home/Delete/5 public ActionResult Delete(int id = 0)
{
T_UserInfo t_userinfo = db.T_UserInfo.Single(t => t.id == id);
if (t_userinfo == null)
{
return HttpNotFound();
}
return View(t_userinfo);
} //
// POST: /Home/Delete/5 [HttpPost, ActionName("Delete")]
public ActionResult DeleteConfirmed(int id)
{
T_UserInfo t_userinfo = db.T_UserInfo.Single(t => t.id == id);
db.T_UserInfo.DeleteObject(t_userinfo);
db.SaveChanges();
return RedirectToAction("Index");
} protected override void Dispose(bool disposing)
{
db.Dispose();
base.Dispose(disposing);
}
}
}
Index 视图 (这是首页)
@model IEnumerable<MvcApplication4.Models.T_UserInfo>--------------请注意这么一条语句,很重要
@{
ViewBag.Title = "Index";
}
<h2>首页</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
<th>
@* @Html.DisplayNameFor(model => model.Name)*@
@Html.Label("姓名")
</th>
<th>
@* @Html.DisplayNameFor(model => model.Age)*@
@Html.Label("年龄")
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Age)
</td>
<td>
@Html.ActionLink("编辑", "Edit", new { id=item.id }) |
@Html.ActionLink("明细", "Details", new { id=item.id }) |
@Html.ActionLink("删除", "Delete", new { id=item.id })
</td>
</tr>
}
</table>
Create视图
@model MvcApplication4.Models.T_UserInfo
@{
ViewBag.Title = "Create";
}
<h2>创建</h2>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>T_UserInfo</legend>
<div class="editor-label">
@Html.LabelFor(model => model.Name)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Age)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Age)
@Html.ValidationMessageFor(model => model.Age)
</div>
<p>
<input type="submit" value="确定加入" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("跳转到首页", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
delete 视图
@model MvcApplication4.Models.T_UserInfo
@{
ViewBag.Title = "Delete";
}
<h2>删除</h2>
<h3>你确定要删除它吗??</h3>
<fieldset>
<legend>T_UserInfo</legend>
<table>
<tr><th>@Html.Label("姓名:")</th><td>@Html.DisplayFor(model => model.Name)</td></tr>
<tr><th>@Html.Label("年龄:")</th><td> @Html.DisplayFor(model => model.Age)</td></tr>
</table>
</fieldset>
@using (Html.BeginForm()) {
<p>
<input type="submit" value="确定删除" /> |
@Html.ActionLink("跳转到首页", "Index")
</p>
}
Details视图
@model MvcApplication4.Models.T_UserInfo
@{
ViewBag.Title = "Details";
}
<h2>明细</h2>
<fieldset>
<legend>T_UserInfo</legend>
<table>
<tr><th>@Html.Label("姓名:")</th><td>@Html.DisplayFor(model => model.Name)</td></tr>
<tr><th>@Html.Label("年龄:")</th><td> @Html.DisplayFor(model => model.Age)</td></tr>
</table>
</fieldset>
<p>
@Html.ActionLink("编辑", "Edit", new { id=Model.id }) |
@Html.ActionLink("跳转到首页", "Index")
</p>
Edit 视图
@model MvcApplication4.Models.T_UserInfo
@{
ViewBag.Title = "Edit";
}
<h2>编辑</h2>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>T_UserInfo</legend>
@Html.HiddenFor(model => model.id)
<div class="editor-label">
@* @Html.LabelFor(model => model.Name)*@
@Html.Label("姓名")
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</div>
<div class="editor-label">
@*@Html.LabelFor(model => model.Age)*@
@Html.Label("年龄")
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Age)
@Html.ValidationMessageFor(model => model.Age)
</div>
<p>
<input type="submit" value="保存" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("跳转到首页", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
MVC [Control与View交互]的更多相关文章
- Asp.Net MVC Control向View传值
1.通过View(Parameter)参数传值 Control: namespace MyMVCDemo.Controllers { public class PersonControlle ...
- MVC:Control与View传值
MVC页面传值的方式主要有三种: 第一种: 采用ViewData.采用键值对的方式,ViewData存储的是一个object类型,传到view层需要强类型转换:使用起来类似于字典集合模式: ViewD ...
- MVC(Model View Controller)框架
MVC框架 同义词 MVC一般指MVC框架 MVC全名是Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写,一种软件设计典范,用一 ...
- ASP.NET MVC与Sql Server交互,把字典数据插入数据库
在"ASP.NET MVC与Sql Server交互, 插入数据"中,在Controller中拼接sql语句.比如: _db.InsertData("insert int ...
- MVC中视图View向控制器传值的方法
MVC中视图View向控制器传值的方法步骤如下: 1.index页面: 页面中只需要一个触发事件的按钮
- MVC(Model(模型) View(视图) Controller(控制器))
复习 1. 商品表 增删改查 index.php add.php view.php edit.php action.php 2. MVC(Model(模型) Vie ...
- ASP.NET MVC Controller向View传值的几种方式
上几篇博文提到MVC和WebForm的区别,主要是MVC的Controller和View将传统的WebForm的窗体和后台代码做了解耦,这篇博文简单介绍一下在MVC中Controller向View是如 ...
- ASP.NET没有魔法——ASP.NET MVC Razor与View渲染
对于Web应用来说,它的界面是由浏览器根据HTML代码及其引用的相关资源进行渲染后展示给用户的结果,换句话说Web应用的界面呈现工作是由浏览器完成的,Web应用的原理是通过Http协议从服务器上获取到 ...
- ASP.NET没有魔法——ASP.NET MVC Razor与View渲染 ASP.NET没有魔法——ASP.NET MVC界面美化及使用Bundle完成静态资源管理
ASP.NET没有魔法——ASP.NET MVC Razor与View渲染 对于Web应用来说,它的界面是由浏览器根据HTML代码及其引用的相关资源进行渲染后展示给用户的结果,换句话说Web应用的 ...
随机推荐
- list_entry(ptr, type, member)——知道结构体内某一成员变量地址,求结构体地址
#define list_entry(ptr, type, member) \ ((type *)(() -> member))) 解释: 1 在0这个地址看做有一个虚拟的type类型的变量,那 ...
- Spring集成Redis集群(含spring集成redis代码)
代码地址如下:http://www.demodashi.com/demo/11458.html 一.准备工作 安装 Redis 集群 安装参考: http://blog.csdn.net/zk6738 ...
- json servlet通信 显示数据
servlet //输出JSON格式的省份信息 @WebServlet("/ServletDemo1") public class ServletDemo1 extends Htt ...
- 【Shiro】Apache Shiro架构之权限认证(Authorization)
Shiro系列文章: [Shiro]Apache Shiro架构之身份认证(Authentication) [Shiro]Apache Shiro架构之集成web [Shiro]Apache Shir ...
- java基础讲解09-----接口,继承,多态
还有什么包装类,数字类,这些简单的我就不想过去介绍,前面也大概的介绍了下,继承,多态 1.类的继承 继承的思想:基于某个父类的扩展,制定一个新的子类.子类可以继承父类原有的属性,方法,也可以重写父类的 ...
- ss - float浮动模块的高度问题 解决方案
当一个Div中的子元素都是浮动元素时,该div是没有高度的.通常会带来很多困扰,解决方案如下: 低版本统配兼容: overflow: hidden; 下面是不支持低配浏览器,而且似乎该效果对 P 标签 ...
- php-fig组织fig-standards的一些标准
参考: http://psr.phphub.org/ https://github.com/php-fig/fig-standards https://github.com/PizzaLiu/PHP- ...
- Angularjs promise-$q服务详解
var ngApp=angular.module('ngApp',[]); /************************************************************* ...
- 安装Eclipse插件长时间卡在 calculating requirements and dependencies
把"Contact all update sites during install to find required software"前面的勾去掉,然后点击下一步,这样之后问题迎 ...
- poj Ping pong LA 4329 (树状数组统计数目)
Ping pong Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2302 Accepted: 879 Descript ...