首页 > 代码库 > MVC数据修改
MVC数据修改
1、视图(显示)界面代码:
@{ Layout = null;}@using Mvc1.Models;<!DOCTYPE html>@{ if(Session["user"]==null) { Response.Redirect("Login"); } }<html><head> <meta name="viewport" content="width=device-width" /> <title>Index</title></head><body> <div> <table style="background-color: navy; text-align: center; width: 100%;"> <tr style="color: white;"> <td>用户名</td> <td>密码</td> <td>昵称</td> <td>性别</td> <td>生日</td> <td>民族</td> <td>操作</td> </tr> @{ //先将数据取出来 List<Users> list = new UsersData().Select(); foreach (Users u in list) { <tr style="background-color: #e0e0e0;"> <td>@u.UserName</td> <td>@u.PassWord</td> <td>@u.NickName</td> <td>@((bool)u.Sex ? "男" : "女")</td> <td>@Convert.ToDateTime(u.Birthday).ToString("yyyy年MM月dd日")</td> <td>@u.NationName</td> <td> <a href=http://www.mamicode.com/"Home/Update/@u.UserName">修改</a> </td> </tr> } } </table> </div></body></html>
2、视图(修改)界面代码:
@{ Layout = null;}@using Mvc1.Models;@model Users<!DOCTYPE html><html><head> <meta name="viewport" content="width=device-width" /> <title>Update</title></head><body> <form name="form1" method="post" action="~/Home/UUU"> 用户名:<input type="text" name="username" value=http://www.mamicode.com/"@Model.UserName" /><br /> 密码:<input type="text" name="password" value=http://www.mamicode.com/"@Model.PassWord" /><br /> 昵称:<input type="text" name="nickname" value=http://www.mamicode.com/"@Model.NickName" /><br /> 性别: @{ if ((bool)Model.Sex) { <input type="radio" name="sex" checked="checked" value=http://www.mamicode.com/"True" />@:男 <input type="radio" name="sex" value=http://www.mamicode.com/"False" />@:女 } else { <input type="radio" name="sex" value=http://www.mamicode.com/"True" />@:男 <input type="radio" name="sex" checked="checked" value=http://www.mamicode.com/"False" />@:女 } } <br /> 生日: <select id="year" onchange="DateChange()"> @{ for (int i = DateTime.Now.Year; i > 1900; i--) { if (Convert.ToDateTime(Model.Birthday).Year == i) { <option selected="selected">@i</option> } else { <option>@i</option> } } } </select>年 <select id="month" onchange="DateChange()"> @{ for (int i = 1; i <= 12; i++) { if (Convert.ToDateTime(Model.Birthday).Month == i) { <option selected="selected">@i</option> } else { <option>@i</option> } } } </select>月 <select id="day" onchange="DateChange()"> @{ for (int i = 1; i <= 31; i++) { if (Convert.ToDateTime(Model.Birthday).Day == i) { <option selected="selected">@i</option> } else { <option>@i</option> } } } </select>日 <input type="text" id="birthday" name="birthday" style="display:none;" value=http://www.mamicode.com/"@Model.Birthday" /> <br /> 民族: <select name="nation"> @{ List<Nation> Nlist = new NationData().Select(); foreach (var n in Nlist) { if (Model.Nation == n.NationCode) { <option value=http://www.mamicode.com/"@n.NationCode" selected="selected">@n.NationName</option> } else { <option value=http://www.mamicode.com/"@n.NationCode">@n.NationName</option> } } } </select> <br /> <input type="button" value=http://www.mamicode.com/"提交" onclick="form1.submit()" /> </form></body></html><script type="text/javascript"> function DateChange() { var year = document.getElementById(‘year‘).value; var month = document.getElementById(‘month‘).value; var day = document.getElementById(‘day‘).value; document.getElementById(‘birthday‘).value = http://www.mamicode.com/year + "-" + month + "-" + day; }</script>
3、控制层控制器代码:
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Mvc;using Mvc1.Models;namespace Mvc1.Controllers{ public class HomeController : Controller { public ActionResult Index() { return View(); } public ActionResult Update(string id) { Users u = new UsersData().Select(id); return View(u); } public ActionResult UUU(Users data) { new UsersData().Update(data); return RedirectToAction("Index", "Home"); } }}
4、模型层代码:
民族表查询
using System;using System.Collections.Generic;using System.Linq;using System.Web;namespace Mvc1.Models{ public class NationData { Data0425DataContext con = new Data0425DataContext(); public List<Nation> Select() { return con.Nation.ToList(); } }}
民族字段扩展
using System;using System.Collections.Generic;using System.Linq;using System.Web;namespace Mvc1.Models{ public partial class Users { Data0425DataContext con = new Data0425DataContext(); public string NationName { get { return con.Nation.Where(r => r.NationCode == this.Nation).FirstOrDefault().NationName; } } }}
数据操作方法
using System;using System.Collections.Generic;using System.Linq;using System.Web;namespace Mvc1.Models{ public class UsersData { Data0425DataContext con = new Data0425DataContext(); public List<Users> Select() { return con.Users.ToList(); } public Users Select(string uname) { return con.Users.Where(r => r.UserName == uname).FirstOrDefault(); } public void Update(Users uuu) { Users u = con.Users.Where(r => r.UserName == uuu.UserName).FirstOrDefault(); u.PassWord = uuu.PassWord; u.NickName = uuu.NickName; u.Sex = uuu.Sex; u.Birthday = uuu.Birthday; u.Nation = uuu.Nation; con.SubmitChanges(); } }}
MVC数据修改
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。