新的开始 接触ASP.NET Core跨平台的框架
今天我刚学习了使用Visual Studio 2015新建了.NET Core项目写了一个小的CSHTML代码。按我的话说就是,把C#和HTML合起来使用了,
写了一个简单的关于学生的“增” “删” “改" "查"的一系列的代码(只是一个简单的关于的学生的“增” “删” “改" "查"代码哦)。
今天可以说是我第一次接触CSHTML,学会了把简单的代码在网页上显示出来。
这种技术叫ASP.NET Core是一个全新的跨平台的框架,主要用于架构如Web应用等连接互联网的现代应用程序,可以在Windows上跨平台
开发和运行你的ASP.NET Core应用。
一开始就从数据库查询所须的条件
public IActionResult Index()
{
var list = new List<Dictionary<string, dynamic>>(); //用字典接收每一个事物的所有值
using (var con = new SqlConnection("server=.;database=MySchool;uid=sa;pwd=ljk123;"))
//连接数据库
{
var cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = @"select StudentNo,StudentName,Sex,t2.GradeName from Student t1 ,Grade t2
where t1.GradeId= t2.GradeId";
//按传递的Sql语句执行操作
con.Open();
var dr = cmd.ExecuteReader();
while (dr.Read())
{
list.Add(Enumerable.Range(0, dr.FieldCount).ToDictionary(dr.GetName, dr.GetValue));
}
//循环遍历
}
ViewData["Persons"] = list; //将遍历出来的值赋到ViewData["变量名"]中。
return View();
}
然后在Imdex中按表单的形式查询所查学生的信息
<table border="0" title="添加">
<caption>添加信息</caption>
<tr>
<th>学号</th>
<th>姓名</th>
<th>性别</th>
<th>年级</th>
</tr>
<tr>
<td imput type=text></td>
<td imput type=text></td>
<td imput type=text></td>
<td imput type=text></td>
</tr>
</table>
<table class="table table-bordered table-hover table-condensed table-responsive table-striped">
<thead>
<tr>
<th>学号</th>
<th>姓名</th>
<th>性别</th>
<th>年级</th>
<th>操作</th>
</tr>
</thead>
<tbody>
@foreach (var item in ViewData["Persons"] as System.Collections.Generic.List<Dictionary<string,dynamic>>)
{
<tr>
<td>@item["StudentNo"]</td>
<td>@item["StudentName"]</td>
<td>@item["Sex"]</td>
<td>@item["GradeName"]</td>
<td>
<a class="btn btn-success" href="/home/modify?bh=@item["StudentNo"]">修改</a>
<button class="btn btn-danger" onclick="deletestu('@item["StudentNo"]')">删除</button>
</td>
</tr>
}
</tbody>
</table>
然后在HomeController中添加学生信息(Get Post)
[HttpGet]
public IActionResult Add()
{
ViewData["Message"] = "Your application description page.";
return View();
}
[HttpPost]
public IActionResult Add(string mc, string bh, string gen, int nj)
{
var list = new List<Dictionary<string, dynamic>>(); //用字典接收每一个事物的所有值
using (var con = new SqlConnection("server=.;database=MySchool;uid=sa;pwd=ljk123;"))
//连接数据库
{
var cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = $@"insert into Student(StudentNo,StudentName,Sex,GradeId)
values('{bh}','{mc}','{gen}',{nj})";
//按传递的Sql语句执行操作
con.Open();
var dr = cmd.ExecuteNonQuery();
if (dr > 0)
{
return Redirect("/home/index");
}
else
{
ViewData["Message"] = "添加失败!";
return View();
}
}
}
在add按表单排版,"添加"新的学生信息
<form method="post" action="/home/add">
<p>学生编号:<input name="bh" class="form-control" type="text"></p>
<p>学生姓名:<input name="mc" class="form-control" type="text"></p>
性       别:
<input name="gen" type="radio" class="input" value="男" checked="checked">男
<input name="gen" type="radio" class="input" value="女">女 <br />
年       级:
<select name="nj">
<option value="1">S1</option>
<option value="2">S2</option>
<option value="3">Y2</option>
<option value="1" selected="selected">S1</option>
</select> <br /><br />
                     
<input type="reset" name="Reset" value="重填">    
<input type="submit" name="Submit" value="提交">
</form>
@ViewData["Message"]
在HomeController中修改学生信息(Get Post)
[HttpGet]
public IActionResult Modify(string bh)
{
ViewData["Message"] = "Your contact page.";
Dictionary<string, dynamic> dic = null;
using (var con = new SqlConnection("server=.;database=MySchool;uid=sa;pwd=ljk123;"))
//连接数据库
{
var cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = $@"select StudentNo,StudentName,Sex,GradeId from Student
where StudentNo= '{bh}'";
//按传递的Sql语句执行操作
con.Open();
var dr = cmd.ExecuteReader();
while (dr.Read())
{
dic = (Enumerable.Range(0, dr.FieldCount).ToDictionary(dr.GetName, dr.GetValue));
}
//循环遍历
}
ViewData["Person"] = dic; //将遍历出来的值赋到ViewData["变量名"]中。
return View();
}
[HttpPost]
public IActionResult Modify(string mc, string bh, string gen, int nj)
{
var list = new List<Dictionary<string, dynamic>>(); //用字典接收每一个事物的所有值
using (var con = new SqlConnection("server=.;database=MySchool;uid=sa;pwd=ljk123;"))
//连接数据库
{
var cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = $@"update Student set StudentNo = '{bh}',StudentName='{mc}',Sex='{gen}',GradeId='{nj}'
where StudentNo = '{bh}'";
//按传递的Sql语句执行操作
con.Open();
var dr = cmd.ExecuteNonQuery();
if (dr > 0)
{
return Redirect("/home/index");
}
else
{
ViewData["Message"] = "更新失败!";
return View();
}
}
return View();
}
通过把学生的编号传过去修改
<a class="btn btn-success" href="/home/modify?bh=@item["StudentNo"]">修改</a>
在Midify中排版修改
<form method="post" action="/home/Modify">
<p>学生编号:<input name="bh" class="form-control" disabled="disabled" type="text" value="@((ViewData["Person"] as Dictionary<string, dynamic>)["StudentNo"])"></p>
<p>学生姓名:<input name="mc" class="form-control" type="text" value="@((ViewData["Person"] as Dictionary<string, dynamic>)["StudentName"])"></p>
性       别:
@if ((ViewData["Person"] as Dictionary<string, dynamic>)["Sex"] == "男")
{
<input name="gen" type="radio" class="input" value="男" checked="checked"><span>男</span>
<input name="gen" type="radio" class="input" value="女"><span>女</span>
}
else
{
<input name="gen" type="radio" class="input" value="男"><span>男</span>
<input name="gen" type="radio" class="input" value="女" checked="checked"><span>女</span>
}
</br>年       级:
<select name="nj">
@if ((ViewData["Person"] as Dictionary<string, dynamic>)["GradeId"] == 1)
{
<option value="1" selected="selected">S1</option>
<option value="2">S2</option>
<option value="3">Y2</option>
}
else if (((ViewData["Person"] as Dictionary<string, dynamic>)["GradeId"] == 2))
{
<option value="1">S1</option>
<option value="2" selected="selected">S2</option>
<option value="3">Y2</option>
}
else
{
<option value="1">S1</option>
<option value="2">S2</option>
<option value="3" selected="selected">Y2</option>
}
</select> <br /><br />
<input type="reset" name="Reset" value="重填">
<input type="submit" name="Submit" value="提交">
</form>
最后是删除了通过接收到的编号进行删除
<button class="btn btn-danger" onclick="deletestu('@item["StudentNo"]')">删除</button>
在HomeController中删除学生信息(post)
[HttpPost]
public string Delete(string bh)
{
ViewData["Message"] = "Your contact page.";
using (var con = new SqlConnection("server=.;database=MySchool;uid=sa;pwd=ljk123;"))
//连接数据库
{
var cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = $@"delete from Student
where StudentNo= '{bh}'";
//按传递的Sql语句执行操作
con.Open();
var dr = cmd.ExecuteNonQuery();
if (dr > 0)
{
return "删除成功!";
}
else
{
return "删除失败!";
}
}
}
最后在用AJAX进行删除
<script> //定义客户端脚本 function deletestu(bh) // function 功能,作用; { if (confirm("确定要删除吗?")) { $.post("/home/delete", { bh: bh }, function (result) { alert(result); window.location.href="/home/index"; }) } } </script>