DAL
/// <summary>
/// 添加
/// </summary>
/// <param name="jia"></param>
/// <returns></returns>
public int add(ShouXiInfo jia)
{
conn.Open();
string sql =string.Format( "insert into shouxi values('{0}','{1}','{2}',getdate())",jia.U_ID,jia.Y_ID,jia.S_ID);
SqlCommand com = new SqlCommand(sql,conn);
var result = com.ExecuteNonQuery();
return result;
}
/// <summary>
/// 查询用户表id名字
/// </summary>
/// <returns></returns>
public List<UserInfo> getuse()
{
string sql = " select U_ID,U_Name from users";
SqlDataAdapter data = new SqlDataAdapter(sql, conn);
DataTable table = new DataTable();
data.Fill(table);
var result = JsonConvert.DeserializeObject<List<UserInfo>>(JsonConvert.SerializeObject(table));
return result;
}
/// <summary>
/// 查询业主表
/// </summary>
/// <returns></returns>
public List<YeZhuInfo> getyezhu()
{
string sql = " select Y_ID,Y_XiaoQu from yezhu";
SqlDataAdapter data = new SqlDataAdapter(sql, conn);
DataTable table = new DataTable();
data.Fill(table);
var result = JsonConvert.DeserializeObject<List<YeZhuInfo>>(JsonConvert.SerializeObject(table));
return result;
}
/// <summary>
/// 查询收费项目表
/// </summary>
/// <returns></returns>
public List<ShouFeiInfo> getshoufei()
{
string sql = " select S_ID,S_Name from shoufei";
SqlDataAdapter data = new SqlDataAdapter(sql, conn);
DataTable table = new DataTable();
data.Fill(table);
var result = JsonConvert.DeserializeObject<List<ShouFeiInfo>>(JsonConvert.SerializeObject(table));
return result;
}
BLL
/// <summary>
/// 添加
/// </summary>
/// <param name="jia"></param>
/// <returns></returns>
public int add(ShouXiInfo jia)
{
return ud.add(jia);
}
/// <summary>
/// 查询用户表id名字
/// </summary>
/// <returns></returns>
public List<UserInfo> getuse()
{
return ud.getuse();
}
/// <summary>
/// 查询业主表
/// </summary>
/// <returns></returns>
public List<YeZhuInfo> getyezhu()
{
return ud.getyezhu();
}
/// <summary>
/// 查询收费项目表
/// </summary>
/// <returns></returns>
public List<ShouFeiInfo> getshoufei()
{
return ud.getshoufei();
}
控制器
public ActionResult Add()
{
//获取用户表
var data = ub.getuse();
SelectList slist = new SelectList(data, "U_ID", "U_Name");
ViewBag.user = slist;
//获取业主表
var data1 = ub.getyezhu();
SelectList slist1 = new SelectList(data1, "Y_ID", "Y_XiaoQu");
ViewBag.yezhu = slist1;
//获取项目表
var data2 = ub.getshoufei();
SelectList slist2 = new SelectList(data2, "S_ID", "S_Name");
ViewBag.shoufei = slist2;
return View();
}
[HttpPost]
public ActionResult Add(ShouXiInfo s)
{
ShouXiInfo shou = new ShouXiInfo();
shou.U_ID = s.U_ID;
shou.Y_ID = s.Y_ID;
shou.S_ID = s.S_ID;
var result = ub.add(shou);
if (result > 0)
{
return Content("<script>alert('添加成功');location.href='/Jia/show'</script>");
}
else
{
return Content("<script>alert('添加失败')</script>");
}
}
视图
@model Model.ShouXiInfo
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Add</title>
</head>
<body>
<div>
@using (Html.BeginForm())
{
<h2>添加页面</h2>
<table>
<tr>
<td>业主姓名</td>
<td>@Html.DropDownListFor(T=>T.U_ID, ViewBag.user as SelectList)</td>
</tr>
<tr>
<td>小区</td>
<td>@Html.DropDownListFor(T => T.Y_ID, ViewBag.yezhu as SelectList)</td>
</tr>
<tr>
<td>业主姓名</td>
<td>@Html.DropDownListFor(T => T.S_ID, ViewBag.shoufei as SelectList)</td>
</tr>
<tr>
<td colspan="2"><input id="Submit1" type="submit" value="提交" /></td>
</tr>
</table>
}
</div>