MVC 数据库增删改查(Razor)视图(2)

(一)主页面
@using MvcApplication1.Models; @model List
<student> @{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <title>Index</title> <style type="text/css"> .tbhead { background-color:navy; text-align:center; color:white; font-weight:bold; } .tbrow1 { text-align:center; background-color:#FFFFcc; } .tbrow2 { text-align:center; background-color:#ccFFFF; } </style> </head> <body> <div> <table id="tbstu" width="100%" cellpadding="4" cellspacing="1" border="0"> <tr class="tbhead"> <td>学号</td> <td>姓名</td> <td>性别</td> <td>生日</td> <td>班级</td> <td>操作</td> </tr> @{ int i=0; foreach(student s in Model) { if(i%2==0) { <tr class="tbrow1"> <td>@s.Sno</td> <td>@s.Sname</td> <td><img src="@(s.Ssex=="" ? "images/nan.png":"images/nv.png")" /> </td> <td>@s.Sbirthday.Value.ToLongDateString().ToString()</td> <td>@s.Class</td> <td> @* <a href="/Home/xiugai/@s.Sno">修改</a>*@ @Html.ActionLink("修改", "xiugai", "Home", new { id=s.Sno},null) @* <a href="/Home/Delete/@s.Sno" οnclick="return confirm('确认删除 @s.Sname 吗?')">删除</a>*@ @Html.ActionLink("删除", "Delete", "Home", new { id = s.Sno }, new { οnclick="return confirm('确定要删除吗?')"}) </td> </tr> } else { <tr class="tbrow2"> <td>@s.Sno</td> <td>@s.Sname</td> <td><img src="@(s.Ssex=="" ? "images/nan.png":"images/nv.png")" /></td> <td>@s.Sbirthday.Value.ToLongDateString().ToString()</td> <td>@s.Class</td> <td> @Html.ActionLink("修改", "xiugai", "Home", new { id=s.Sno} ,null) @Html.ActionLink("删除", "Delete", "Home", new { id = s.Sno }, new { οnclick="return confirm('确定要删除吗?')"}) </td> </tr> } i++; } } </table> <form action="/Home/zengjia" method="post"> <input type="submit" value="添加" /> </form> </div> </body> </html>

(二) 修改页面

@using MvcApplication1.Models; @model student @{ Layout
= null; } <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <title>xiugai</title> <style type="text/css"> .uu { list-style:none; } </style> </head> <body> <div> <form id="f1" name="f1" action="/Home/Update" method="post"> @{ if (Model!=null) { <ul class="uu"> <li>学号:@*<input type="text" name="sno" readonly="readonly" value="@s.Sno" />*@ @Html.TextBoxFor(p => p.Sno, new { }) </li> <li>姓名:@Html.TextBoxFor(p=>p.Sname)</li> <li>性别:@Html.RadioButtonFor(p=>p.Ssex,"男")男 @Html.RadioButtonFor(p=>p.Ssex,"女")女 </li> <li>生日:@Html.TextBoxFor(p=>p.Sbirthday)</li> <li>班级:@Html.TextBoxFor(p=>p.Class)</li> </ul> <input type="submit" name="submit" value="更新" /> } } </form> <form action="/Home/tiaozhuan" method="post"> <input type="submit" value="回到主页" /> </form> </div> </body> </html>
(三)增加页面

@using MvcApplication1.Models; @model student @{ Layout
= null; } <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <title>zengjia</title> </head> <body> <div> <h1>添加学生信息</h1> <form id="f1" name="f1" action="/Home/ADD" method="post"> <ul class="uu"> <li>学号:@Html.TextBoxFor(p=>p.Sno)</li> <li>姓名:@Html.TextBoxFor(p=>p.Sname)</li> <li>性别:@Html.RadioButtonFor(p => p.Ssex, "男") 男 @Html.RadioButtonFor(p=>p.Ssex,"女") 女</li> <li>生日:@Html.TextBoxFor(p=>p.Sbirthday)</li> <li>班级:@Html.TextBoxFor(p=>p.Class)</li> </ul> <input type="submit" name="submit" value="添加" /> </form> <form action="/Home/tiaozhuan" method="post"> <input type="submit" value="回到主页" /> </form> </div> </body> </html>

(四) 出错显示界面
@{ Layout
= null; } <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <title>chucuo</title> </head> <body> <div> <h1>出错了!</h1> <form action="/Home/tiaozhuan" method="post"> <input type="submit" value="回到主页" /> </form> </div> </body> </html>

 

转载于:https://www.cnblogs.com/w-wz/p/4630606.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MVC模式的实现对数据库增删改查 部分代码: package dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.ArrayList; import java.util.List; import common.DBConnection; import bean.Contact; public class ContactDAO { public List getAllContact() throws Exception{ Connection conn=DBConnection.getConntion(); PreparedStatement ps=conn.prepareStatement("select * from Contact"); ResultSet rs=ps.executeQuery(); List list = new ArrayList(); while(rs.next()){ int id = rs.getInt("id"); String name = rs.getString("name"); String phone = rs.getString("phone"); String address = rs.getString("address"); Contact c = new Contact(); c.setId(id); c.setName(name); c.setPhone(phone); c.setAddress(address); list.add(c); } rs.close(); ps.close(); conn.close(); return list; } public void addContact(String name,String phone,String address) throws Exception{ String sql = "insert into contact(id,name,phone,address) values(seq_contact.nextval,?,?,?)"; Connection con = DBConnection.getConntion(); PreparedStatement pstmt = con.prepareStatement(sql); pstmt.setString(1, name); pstmt.setString(2, phone); pstmt.setString(3, address); pstmt.executeUpdate(); } public void delContact(int id) throws Exception{ String sql = "delete from contact where id=?"; Connection con = DBConnection.getConntion(); PreparedStatement pstmt = con.prepareStatement(sql); pstmt.setInt(1, id); pstmt.executeUpdate(); } public Contact getContactById(int id) throws Exception{ String sql = "select * from Contact where id=?"; Connection con = DBConnection.getConntion(); PreparedStatement pstmt = con.prepareStatement(sql); pstmt.setInt(1, id); ResultSet rs = pstmt.executeQuery(); Contact c = null; while(rs.next()){ // int id = rs.getInt("id"); String name=rs.getString("name"); String p

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值