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")
}



时间: 2024-11-09 00:29:43

MVC [Control与View交互]的相关文章

MVC [Control与View交互]2

<1> 控制器 Home using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using MvcApplication1.Models; namespace MvcApplication1.Controllers { public class HomeController : Controller { private salesEnt

Asp.Net MVC Control向View传值

 1.通过View(Parameter)参数传值     Control: namespace MyMVCDemo.Controllers { public class PersonController : Controller { // // GET: /Person/ public ActionResult Index() { Person whiteperson = new Person(); whiteperson.Id = 1; whiteperson.Name = "James&qu

MVC:Control与View传值

MVC页面传值的方式主要有三种: 第一种: 采用ViewData.采用键值对的方式,ViewData存储的是一个object类型,传到view层需要强类型转换:使用起来类似于字典集合模式: ViewData["key"]="value" 第二种: 采用ViewBag.ViewBag是ViewData模式的一部分,采用模式也就是ViewBag.key=value. PS: 1.关于ViewData和ViewBag对比如下 ViewData与ViewBag对比: Vie

ASP.NET MVC与Sql Server交互,把字典数据插入数据库

在"ASP.NET MVC与Sql Server交互, 插入数据"中,在Controller中拼接sql语句.比如: _db.InsertData("insert into Product(Name,quantity,Price) values('"+productVm.Name+"','"+productVm.Quantity+"','"+productVm.Price+"')"); 在某些场景中需要把数

spring mvc 和ajax异步交互完整实例(转自CSDN) 附下载地址

spring mvc 和ajax异步交互完整实例 spring MVC 异步交互demo: demo下载地址:http://download.csdn.net/download/quincylk/9521375 1.jsp页面: [java] view plain copy print? <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-

MVC在页面View上获取当前控制器名称、Action名称以及路由参数

有时候在封装MVC通用控件时需要在页面上获取这些数据. 用以下方法即可: //获取控制器名称: ViewContext.RouteData.Values["controller"].ToString(); //获取Action名称: ViewContext.RouteData.Values["action"].ToString(); //获取路由参数值: ViewContext.RouteData.Values[名称].ToString(); //如:ViewCon

ASP.NET MVC Controller向View传值的几种方式

1)ViewBag ViewBag是动态类型,使用时直接添加属性赋值即可 ViewBag.myName 控制器代码: 1 public ActionResult Index() 2 { 3 ViewBag.name = "梁顺盛"; 4 ViewBag.message = "欢迎使用MVC设计模式~~"; 5 return View(); 6 } 视图代码: <div> <!--利用HtmlHelper创建TextBox时,使用名称与ViewBag

QT MVC 技术Model/View初探

Model/View实现表格技术 [+] 一.简介 Model/View结构使数据管理与相应的数据显示相互独立,并提供了一系列标准的函数接口和用于Model模块与View模块之间的通信.它从MVC演化而来,MVC由三种对象组成,Model是应用程序对象,View是它的屏幕表示,Controller定义了用户界面如何对用户输入进行响应.把MVC中的View和Controller合在一起,就形成了Model/View结构. 二.运行图 (1)为了灵活对用户的输入进行处理,引入了Delegate,Mo

ASP.NET没有魔法——ASP.NET MVC Razor与View渲染

对于Web应用来说,它的界面是由浏览器根据HTML代码及其引用的相关资源进行渲染后展示给用户的结果,换句话说Web应用的界面呈现工作是由浏览器完成的,Web应用的原理是通过Http协议从服务器上获取到对应的Html代码以及相关资源,使得浏览器能够完成正确的呈现工作. ASP.NET MVC作为一个Web应用构建框架View承担了UI显示的功能,在开发过程中View以Action的名称命名,当用户的请求被路由到某一Action方法时,ASP.NET MVC将会根据Action的名称来获取到对应的V